Javascript solution


  • 0
    L
    var arrayNesting = function(nums) {
        var len = nums.length,i,max = 0,count = 0;
        var set = new Set();
        for(i=0;i<len;i++){
            while(set.has(i) == false){
                set.add(i);
                i = nums[i];
                count++;
            }
            max = max > count ? max : count;
            count = 0;
        }
        return max;
    };
    

    Pay attention to define set. After each iteration, remember to change count to zero.
    Sorry for my poor English.


Log in to reply
 

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