0565.cpp (466B)
1 class Solution { 2 public: 3 int arrayNesting(const vector<int> &nums) const { 4 static int seen[100001]; 5 6 const int n = size(nums); 7 memset(seen, 0x00, n * sizeof(int)); 8 9 int res = 0; 10 for (int i = 0; i < size(nums); i++) { 11 int crnt = i, size = 0; 12 while (!seen[crnt]) 13 size++, seen[crnt] = true, crnt = nums[crnt]; 14 res = max(res, size); 15 } 16 return res; 17 } 18 };