public class Solution {
public int arrayNesting(int[] nums) {
if (nums == null || nums.length < 1) return 0;
Set<Integer> set = new HashSet<>();
int res = Integer.MIN_VALUE;
for(int num : nums) {
if (set.contains(num)) continue;
int temp = num;
int count = 0;
while (!set.contains(temp)) {
set.add(temp);
temp = nums[temp];
count++;
}
res = Math.max(res, count);
}
return res;
}
}
public class Solution {
public int arrayNesting(int[] nums) {
int maxSize = 0;
for (int i = 0; i < nums.length; i++) {
int size = 0;
for (int k = i; nums[k] >= 0; size++) {
int ak = nums[k];
nums[k] = -1;
k = ak;
}
maxSize = Integer.max(maxSize, size);
}
return maxSize;
}
}