35ms java simple solution


  • 0
    F

    Just change the visited value to negative. Once we get the negative value, stop the while loop and compare the result with max.

    public class Solution {
        public int arrayNesting(int[] nums) {
            int max=0;
            for(int i=0; i<nums.length;i++){
                int j=i,count=0;
                while(nums[j]!=-1){
                    int temp=j;
                    j=nums[j];
                    nums[temp]=-1;
                    count++;
                }
                if(count>max) max=count;
            }
            return max;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.