565. Array Nesting 發表於 2023-02-13 | 分類於 leetcode problemsolutiondetect cycle 123456789101112131415161718class Solution {public: int arrayNesting(vector<int>& nums) { int n = nums.size(), ret = 0; vector<bool> visited(n, false); for(int i=0;i<n;++i){ int size = 0, j = i; while( visited[j] == false){ visited[j] = true; j = nums[j]; size++; } ret = max(ret, size); } return ret; }}; option 2 - constant space123456789101112131415161718class Solution {public: int arrayNesting(vector<int>& nums) { int n = nums.size(), ret = 0; for(int i=0;i<n;++i){ int size = 0, j = i; while( nums[j] >= 0){ int idx = nums[j]; nums[j] = -1; j = idx; size++; } ret = max(ret, size); } return ret; }}; analysis time complexity O(n) space complexity O(1)