Super concise C++ solution


  • 0
    Z
    class Solution {
    public:
        int arrayNesting(vector<int>& nums) {
            int res = 1, size = nums.size();
            for (int i = 0; i < size; ++i) {
                if (nums[i] == i || nums[i] < 0) continue;
                int k = i, len = 0;
                while (nums[k] >= 0) {
                    nums[k] = -nums[k];
                    k = -nums[k];
                    ++len;
                }
                res = max(res, len);
            }
            return res;
        }
    };
    

Log in to reply
 

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