java 565. Array Nesting(1st).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 565. Array Nesting(1st).java相关的知识,希望对你有一定的参考价值。

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;
    }
}

以上是关于java 565. Array Nesting(1st).java的主要内容,如果未能解决你的问题,请参考以下文章

java 565. Array Nesting(1st).java

java 565. Array Nesting(1st).java

java 565. Array Nesting(1st).java

java 565. Array Nesting(1st).java

565. Array Nesting

565. Array Nesting