Skip to content

Commit 05386b8

Browse files
committed
feat: add solutions to lc problem: No.0565
No.0565.Array Nesting
1 parent b98f057 commit 05386b8

File tree

6 files changed

+210
-59
lines changed

6 files changed

+210
-59
lines changed

solution/0500-0599/0565.Array Nesting/README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40-
嵌套数组最终一定会形成一个环,在枚举 `S[i]` 的过程中,可以用 `vis` 数组剪枝,避免重复枚举同一个环
40+
**方法一:图**
41+
42+
嵌套数组最终一定会形成一个环,在枚举 $nums[i]$ 的过程中,可以用 $vis$ 数组剪枝,避免重复枚举同一个环。
43+
44+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
45+
46+
**方法二:原地标记**
47+
48+
由于 $nums$ 元素均在 $[0..n-1]$ 之间,因此,对于访问过的元素,我们可以令 $nums[i]=n$,从而省略 $vis$ 数组。
49+
50+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
4151

4252
<!-- tabs:start -->
4353

@@ -64,6 +74,21 @@ class Solution:
6474
return res
6575
```
6676

77+
```python
78+
class Solution:
79+
def arrayNesting(self, nums: List[int]) -> int:
80+
ans, n = 0, len(nums)
81+
for i in range(n):
82+
cnt = 0
83+
while nums[i] != n:
84+
j = nums[i]
85+
nums[i] = n
86+
i = j
87+
cnt += 1
88+
ans = max(ans, cnt)
89+
return ans
90+
```
91+
6792
### **Java**
6893

6994
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -92,6 +117,26 @@ class Solution {
92117
}
93118
```
94119

120+
```java
121+
class Solution {
122+
public int arrayNesting(int[] nums) {
123+
int ans = 0, n = nums.length;
124+
for (int i = 0; i < n; ++i) {
125+
int cnt = 0;
126+
int j = i;
127+
while (nums[j] < n) {
128+
int k = nums[j];
129+
nums[j] = n;
130+
j = k;
131+
++cnt;
132+
}
133+
ans = Math.max(ans, cnt);
134+
}
135+
return ans;
136+
}
137+
}
138+
```
139+
95140
### **C++**
96141

97142
```cpp
@@ -117,6 +162,29 @@ public:
117162
};
118163
```
119164
165+
```cpp
166+
class Solution {
167+
public:
168+
int arrayNesting(vector<int>& nums) {
169+
int ans = 0, n = nums.size();
170+
for (int i = 0; i < n; ++i)
171+
{
172+
int cnt = 0;
173+
int j = i;
174+
while (nums[j] < n)
175+
{
176+
int k = nums[j];
177+
nums[j] = n;
178+
j = k;
179+
++cnt;
180+
}
181+
ans = max(ans, cnt);
182+
}
183+
return ans;
184+
}
185+
};
186+
```
187+
120188
### **Go**
121189

122190
```go
@@ -143,6 +211,25 @@ func arrayNesting(nums []int) int {
143211
}
144212
```
145213

214+
```go
215+
func arrayNesting(nums []int) int {
216+
ans, n := 0, len(nums)
217+
for i := range nums {
218+
cnt, j := 0, i
219+
for nums[j] != n {
220+
k := nums[j]
221+
nums[j] = n
222+
j = k
223+
cnt++
224+
}
225+
if ans < cnt {
226+
ans = cnt
227+
}
228+
}
229+
return ans
230+
}
231+
```
232+
146233
### **...**
147234

148235
```

solution/0500-0599/0565.Array Nesting/README_EN.md

+77
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ class Solution:
6969
return res
7070
```
7171

72+
```python
73+
class Solution:
74+
def arrayNesting(self, nums: List[int]) -> int:
75+
ans, n = 0, len(nums)
76+
for i in range(n):
77+
cnt = 0
78+
while nums[i] != n:
79+
j = nums[i]
80+
nums[i] = n
81+
i = j
82+
cnt += 1
83+
ans = max(ans, cnt)
84+
return ans
85+
```
86+
7287
### **Java**
7388

7489
```java
@@ -95,6 +110,26 @@ class Solution {
95110
}
96111
```
97112

113+
```java
114+
class Solution {
115+
public int arrayNesting(int[] nums) {
116+
int ans = 0, n = nums.length;
117+
for (int i = 0; i < n; ++i) {
118+
int cnt = 0;
119+
int j = i;
120+
while (nums[j] < n) {
121+
int k = nums[j];
122+
nums[j] = n;
123+
j = k;
124+
++cnt;
125+
}
126+
ans = Math.max(ans, cnt);
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
98133
### **C++**
99134

100135
```cpp
@@ -120,6 +155,29 @@ public:
120155
};
121156
```
122157
158+
```cpp
159+
class Solution {
160+
public:
161+
int arrayNesting(vector<int>& nums) {
162+
int ans = 0, n = nums.size();
163+
for (int i = 0; i < n; ++i)
164+
{
165+
int cnt = 0;
166+
int j = i;
167+
while (nums[j] < n)
168+
{
169+
int k = nums[j];
170+
nums[j] = n;
171+
j = k;
172+
++cnt;
173+
}
174+
ans = max(ans, cnt);
175+
}
176+
return ans;
177+
}
178+
};
179+
```
180+
123181
### **Go**
124182

125183
```go
@@ -146,6 +204,25 @@ func arrayNesting(nums []int) int {
146204
}
147205
```
148206

207+
```go
208+
func arrayNesting(nums []int) int {
209+
ans, n := 0, len(nums)
210+
for i := range nums {
211+
cnt, j := 0, i
212+
for nums[j] != n {
213+
k := nums[j]
214+
nums[j] = n
215+
j = k
216+
cnt++
217+
}
218+
if ans < cnt {
219+
ans = cnt
220+
}
221+
}
222+
return ans
223+
}
224+
```
225+
149226
### **...**
150227

151228
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22
public:
33
int arrayNesting(vector<int>& nums) {
4-
int n = nums.size();
5-
vector<bool> vis(n);
6-
int res = 0;
7-
for (int i = 0; i < n; ++i) {
8-
if (vis[i]) continue;
9-
int cur = nums[i], m = 1;
10-
vis[cur] = true;
11-
while (nums[cur] != nums[i]) {
12-
cur = nums[cur];
13-
++m;
14-
vis[cur] = true;
4+
int ans = 0, n = nums.size();
5+
for (int i = 0; i < n; ++i)
6+
{
7+
int cnt = 0;
8+
int j = i;
9+
while (nums[j] < n)
10+
{
11+
int k = nums[j];
12+
nums[j] = n;
13+
j = k;
14+
++cnt;
1515
}
16-
res = max(res, m);
16+
ans = max(ans, cnt);
1717
}
18-
return res;
18+
return ans;
1919
}
20-
};
20+
};
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
func arrayNesting(nums []int) int {
2-
n := len(nums)
3-
vis := make([]bool, n)
4-
ans := 0
5-
for i := 0; i < n; i++ {
6-
if vis[i] {
7-
continue
2+
ans, n := 0, len(nums)
3+
for i := range nums {
4+
cnt, j := 0, i
5+
for nums[j] != n {
6+
k := nums[j]
7+
nums[j] = n
8+
j = k
9+
cnt++
810
}
9-
cur, m := nums[i], 1
10-
vis[cur] = true
11-
for nums[cur] != nums[i] {
12-
cur = nums[cur]
13-
m++
14-
vis[cur] = true
15-
}
16-
if m > ans {
17-
ans = m
11+
if ans < cnt {
12+
ans = cnt
1813
}
1914
}
2015
return ans
21-
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
class Solution {
22
public int arrayNesting(int[] nums) {
3-
int n = nums.length;
4-
boolean[] vis = new boolean[n];
5-
int res = 0;
6-
for (int i = 0; i < n; i++) {
7-
if (vis[i]) {
8-
continue;
3+
int ans = 0, n = nums.length;
4+
for (int i = 0; i < n; ++i) {
5+
int cnt = 0;
6+
int j = i;
7+
while (nums[j] < n) {
8+
int k = nums[j];
9+
nums[j] = n;
10+
j = k;
11+
++cnt;
912
}
10-
int cur = nums[i], m = 1;
11-
vis[cur] = true;
12-
while (nums[cur] != nums[i]) {
13-
cur = nums[cur];
14-
m++;
15-
vis[cur] = true;
16-
}
17-
res = Math.max(res, m);
13+
ans = Math.max(ans, cnt);
1814
}
19-
return res;
15+
return ans;
2016
}
2117
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution:
22
def arrayNesting(self, nums: List[int]) -> int:
3-
n = len(nums)
4-
vis = [False] * n
5-
res = 0
3+
ans, n = 0, len(nums)
64
for i in range(n):
7-
if vis[i]:
8-
continue
9-
cur, m = nums[i], 1
10-
vis[cur] = True
11-
while nums[cur] != nums[i]:
12-
cur = nums[cur]
13-
m += 1
14-
vis[cur] = True
15-
res = max(res, m)
16-
return res
5+
cnt = 0
6+
while nums[i] != n:
7+
j = nums[i]
8+
nums[i] = n
9+
i = j
10+
cnt += 1
11+
ans = max(ans, cnt)
12+
return ans

0 commit comments

Comments
 (0)