-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathArrayNesting.java
More file actions
34 lines (27 loc) · 710 Bytes
/
ArrayNesting.java
File metadata and controls
34 lines (27 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package leetcode;
/**
* Created by nikoo28 on 6/3/19 1:43 AM
*/
class ArrayNesting {
private int findSetS(int[] nums) {
boolean[] visited = new boolean[nums.length];
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
int start = nums[i], count = 0;
do {
start = nums[start];
count++;
visited[start] = true;
} while (start != nums[i]);
res = Math.max(res, count);
}
}
return res;
}
public static void main(String[] args) {
ArrayNesting arrayNesting = new ArrayNesting();
int[] arr = new int[]{5, 4, 0, 3, 1, 6, 2};
System.out.println(arrayNesting.findSetS(arr));
}
}