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; // mark a[k] as visited;
                    k = ak;
                }
                maxsize = Math.Max(maxsize, size);
            }

            return maxsize;
    }
}

https://leetcode.com/problems/array-nesting/#/description

posted on 2017-06-02 11:14  Sempron2800+  阅读(122)  评论(0编辑  收藏  举报