Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 307ebf1

Browse files
author
Shuo
authored
Merge pull request #678 from openset/develop
Add: Array Nesting
2 parents aef1ebe + 27e5326 commit 307ebf1

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

‎problems/array-nesting/array_nesting.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
package array_nesting
1+
package problem_565
2+
3+
func arrayNesting(nums []int) int {
4+
ans := 0
5+
for i := range nums {
6+
c := 1
7+
for nums[i] != i {
8+
nums[i], nums[nums[i]] = nums[nums[i]], nums[i]
9+
c++
10+
}
11+
if ans < c {
12+
ans = c
13+
}
14+
}
15+
return ans
16+
}
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
package array_nesting
1+
package problem_565
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type caseType struct {
8+
input []int
9+
expected int
10+
}
11+
12+
func TestArrayNesting(t *testing.T) {
13+
tests := [...]caseType{
14+
{
15+
input: []int{5, 4, 0, 3, 1, 6, 2},
16+
expected: 4,
17+
},
18+
{
19+
input: []int{0, 3, 1, 5, 4, 6, 2},
20+
expected: 5,
21+
},
22+
{
23+
input: []int{6, 2, 5, 4, 0, 3, 1},
24+
expected: 7,
25+
},
26+
{
27+
input: []int{3, 1, 5, 4, 0, 6, 2},
28+
expected: 3,
29+
},
30+
}
31+
for _, tc := range tests {
32+
output := arrayNesting(tc.input)
33+
if output != tc.expected {
34+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
35+
}
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /