classSolution: defarrayNesting(self, nums: List[int]) -> int: ans, n = 0, len(nums) vis = [False] * n for i inrange(n): cnt = 0 whilenot vis[i]: vis[i] = True i = nums[i] cnt += 1 ans = max(ans, cnt) return ans
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intarrayNesting(vector<int> &nums){ int ans = 0, n = nums.size(); vector<int> vis(n); for (int i = 0; i < n; ++i) { int cnt = 0; while (!vis[i]) { vis[i] = true; i = nums[i]; ++cnt; } ans = max(ans, cnt); } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintarrayNesting(int[] nums) { intans=0, n = nums.length; boolean[] vis = newboolean[n]; for (inti=0; i < n; ++i) { intcnt=0; while (!vis[i]) { vis[i] = true; i = nums[i]; ++cnt; } ans = Math.max(ans, cnt); } return ans; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolution { publicintArrayNesting(int[] nums) { int ans = 0, n = nums.Length; bool[] vis = newbool[n]; for (int i = 0; i < n; ++i) { int cnt = 0; while (!vis[i]) { vis[i] = true; i = nums[i]; ++cnt; } ans = Math.Max(ans, cnt); } return ans; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcarrayNesting(nums []int) (ans int) { vis := make([]bool, len(nums)) for i := range vis { cnt := 0 for !vis[i] { vis[i] = true i = nums[i] cnt++ } if cnt > ans { ans = cnt } } return }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define MAX(a, b) ((a) > (b) ? (a) : (b))
intarrayNesting(int* nums, int numsSize){ int ans = 0; bool *vis = (bool *)malloc(sizeof(bool) * numsSize); memset(vis, 0, sizeof(bool) * numsSize); for (int i = 0; i < numsSize; ++i) { int cnt = 0; while (!vis[i]) { vis[i] = true; i = nums[i]; ++cnt; } ans = MAX(ans, cnt); } free(vis); return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var arrayNesting = function(nums) { let ans = 0, n = nums.length; const vis = newArray(n).fill(0); for (let i = 0; i < n; ++i) { let cnt = 0; while (!vis[i]) { vis[i] = true; i = nums[i]; ++cnt; } ans = Math.max(ans, cnt); } return ans; };
复杂度分析
时间复杂度:O(n),其中 n 是数组 nums 的长度。
空间复杂度:O(n)。
方法二:原地标记
利用「nums 中的元素大小在 [0, n-1] 之间」这一条件,我们可以省略 vis 数组,改为标记 nums}[i] = n,来实现和 vis 数组同样的功能。
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defarrayNesting(self, nums: List[int]) -> int: ans, n = 0, len(nums) for i inrange(n): cnt = 0 while nums[i] < n: num = nums[i] nums[i] = n i = num cnt += 1 ans = max(ans, cnt) return ans
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intarrayNesting(vector<int> &nums){ int ans = 0, n = nums.size(); for (int i = 0; i < n; ++i) { int cnt = 0; while (nums[i] < n) { int num = nums[i]; nums[i] = n; i = num; ++cnt; } ans = max(ans, cnt); } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintarrayNesting(int[] nums) { intans=0, n = nums.length; for (inti=0; i < n; ++i) { intcnt=0; while (nums[i] < n) { intnum= nums[i]; nums[i] = n; i = num; ++cnt; } ans = Math.max(ans, cnt); } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolution { publicintArrayNesting(int[] nums) { int ans = 0, n = nums.Length; for (int i = 0; i < n; ++i) { int cnt = 0; while (nums[i] < n) { int num = nums[i]; nums[i] = n; i = num; ++cnt; } ans = Math.Max(ans, cnt); } return ans; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcarrayNesting(nums []int) (ans int) { n := len(nums) for i := range nums { cnt := 0 for nums[i] < n { i, nums[i] = nums[i], n cnt++ } if cnt > ans { ans = cnt } } return }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#define MAX(a, b) ((a) > (b) ? (a) : (b))
intarrayNesting(int* nums, int numsSize){ int ans = 0; for (int i = 0; i < numsSize; ++i) { int cnt = 0; while (nums[i] < numsSize) { int num = nums[i]; nums[i] = numsSize; i = num; ++cnt; } ans = MAX(ans, cnt); } return ans; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var arrayNesting = function(nums) { let ans = 0, n = nums.length; for (let i = 0; i < n; ++i) { let cnt = 0; while (nums[i] < n) { const num = nums[i]; nums[i] = n; i = num; ++cnt; } ans = Math.max(ans, cnt); } return ans; };