Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e042ee7

Browse files
✨feat: add 565
1 parent 4067d7b commit e042ee7

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
| [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-string-ii/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-p88f/) | 简单 | 🤩🤩🤩🤩🤩 |
7575
| [551. 学生出勤记录 I](https://leetcode-cn.com/problems/student-attendance-record-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-i/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-hui7/) | 简单 | 🤩🤩🤩 |
7676
| [556. 下一个更大元素 III](https://leetcode.cn/problems/next-greater-element-iii/) | [LeetCode 题解链接](https://leetcode.cn/problems/next-greater-element-iii/solution/by-ac_oier-99bj/) | 中等 | 🤩🤩🤩🤩 |
77+
| [565. 数组嵌套](https://leetcode.cn/problems/array-nesting/) | [LeetCode 题解链接](https://leetcode.cn/problems/array-nesting/solution/by-ac_oier-n5hd/) | 中等 | 🤩🤩🤩🤩 |
7778
| [566. 重塑矩阵](https://leetcode-cn.com/problems/reshape-the-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reshape-the-matrix/solution/jian-dan-ti-zhong-quan-chu-ji-ke-yi-kan-79gv5/) | 简单 | 🤩🤩🤩 |
7879
| [591. 标签验证器](https://leetcode-cn.com/problems/tag-validator/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/tag-validator/solution/by-ac_oier-9l8z/) | 困难 | 🤩🤩🤩🤩 |
7980
| [594. 最长和谐子序列](https://leetcode-cn.com/problems/longest-harmonious-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-harmonious-subsequence/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-quuh/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[565. 数组嵌套](https://leetcode-cn.com/problems/find-the-closest-palindrome/solution/gong-shui-san-xie-tan-xin-fen-xi-shang-x-vtr6/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
索引从 0ドル$ 开始长度为 `N` 的数组 `A`,包含 0ドル$ 到 $N - 1$ 的所有整数。找到最大的集合 `S` 并返回其大小,其中 $S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }$ 且遵守以下的规则。
10+
11+
假设选择索引为 `i` 的元素 $A[i]$ 为 `S` 的第一个元素,`S` 的下一个元素应该是 $A[A[i]],ドル之后是 $A[A[A[i]]]$ ... 以此类推,不断添加直到 `S` 出现重复的元素。
12+
13+
示例 1:
14+
```
15+
输入: A = [5,4,0,3,1,6,2]
16+
17+
输出: 4
18+
19+
解释:
20+
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
21+
22+
其中一种最长的 S[K]:
23+
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
24+
```
25+
26+
提示:
27+
* `N` 是 $[1, 20,000]$ 之间的整数。
28+
* `A` 中不含有重复的元素。
29+
* `A` 中的元素大小在 $[0, N-1]$ 之间。
30+
31+
---
32+
33+
### 模拟
34+
35+
将 $A[i]$ 与 $A[A[i]]$ 之间看作存在一条有向边,由于所有数范围都在 $[0, N - 1],ドル且不重复,因此至少存在一个环,而问题本质是求所有环的最大长度。
36+
37+
直接根据题意记进行模拟即可,从前往后处理每个 $nums[i],ドル并尝试从 $nums[i]$ 出发遍历其所在的环,为了防止某些环被重复处理,对于当前经过的 $nums[i]$ 标记为 $-1,ドル这样每个数被访问的次数最多不超过 3ドル$ 次,整体复杂度为 $O(n)$。
38+
39+
Java 代码:
40+
```Java
41+
class Solution {
42+
public int arrayNesting(int[] nums) {
43+
int n = nums.length, ans = 0;
44+
for (int i = 0; i < n; i++) {
45+
int cur = i, cnt = 0;
46+
while (nums[cur] != -1) {
47+
cnt++;
48+
int c = cur;
49+
cur = nums[cur];
50+
nums[c] = -1;
51+
}
52+
ans = Math.max(ans, cnt);
53+
}
54+
return ans;
55+
}
56+
}
57+
```
58+
TypeScript 代码:
59+
```TypeScript
60+
function arrayNesting(nums: number[]): number {
61+
let n = nums.length, ans = 0
62+
for (let i = 0; i < n; i++) {
63+
let cur = i, cnt = 0
64+
while (nums[cur] != -1) {
65+
cnt++
66+
const c = cur
67+
cur = nums[cur]
68+
nums[c] = -1
69+
}
70+
ans = Math.max(ans, cnt)
71+
}
72+
return ans
73+
};
74+
```
75+
* 时间复杂度:每个数字被访问的次数为常数次,复杂度为 $O(n)$
76+
* 空间复杂度:$O(1)$
77+
78+
---
79+
80+
### 最后
81+
82+
这是我们「刷穿 LeetCode」系列文章的第 `No.565` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
83+
84+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
85+
86+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
87+
88+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
89+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /