当前位置:网站首页>565. 数组嵌套 : 常规模拟题
565. 数组嵌套 : 常规模拟题
2022-07-17 14:32:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 565. 数组嵌套 ,难度为 中等。
Tag : 「模拟」
索引从 开始长度为 N 的数组 A,包含 到 的所有整数。找到最大的集合 S 并返回其大小,其中 且遵守以下的规则。
假设选择索引为 i 的元素 为 S 的第一个元素,S 的下一个元素应该是 ,之后是 ... 以此类推,不断添加直到 S 出现重复的元素。
示例 1:
输入: A = [5,4,0,3,1,6,2]
输出: 4
解释:
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
其中一种最长的 S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
提示:
N是 之间的整数。A中不含有重复的元素。A中的元素大小在 之间。
模拟
将 与 之间看作存在一条有向边,由于所有数范围都在 ,且不重复,因此至少存在一个环,而问题本质是求所有环的最大长度。
直接根据题意记进行模拟即可,从前往后处理每个 ,并尝试从 出发遍历其所在的环,为了防止某些环被重复处理,对于当前经过的 标记为 ,这样每个数被访问的次数最多不超过 次,整体复杂度为 。
Java 代码:
class Solution {
public int arrayNesting(int[] nums) {
int n = nums.length, ans = 0;
for (int i = 0; i < n; i++) {
int cur = i, cnt = 0;
while (nums[cur] != -1) {
cnt++;
int c = cur;
cur = nums[cur];
nums[c] = -1;
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
TypeScript 代码:
function arrayNesting(nums: number[]): number {
let n = nums.length, ans = 0
for (let i = 0; i < n; i++) {
let cur = i, cnt = 0
while (nums[cur] != -1) {
cnt++
const c = cur
cur = nums[cur]
nums[c] = -1
}
ans = Math.max(ans, cnt)
}
return ans
};
时间复杂度:每个数字被访问的次数为常数次,复杂度为 空间复杂度:
最后
这是我们「刷穿 LeetCode」系列文章的第 No.565 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- Sword finger offer II 041 Average value of sliding window
- 机器人开发--机器人资料汇总
- 早期单片机加密的一些方法 【评论区领取资料】
- Discussion on Euler angle solution of rocket large maneuvering motion
- leetcode-08
- Performance optimization @contented to reduce pseudo sharing
- Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
- 要想组建敏捷团队,这些方法不可少
- How does unity3d use the asset store to download some useful resource packages
- Tier defect detection using full revolutionary network
猜你喜欢

Sword finger offer II 041 Average value of sliding window

leetcode-08

Download of common getshell tools
![[multithreading] detailed explanation of JUC (callable interface, renntrantlock, semaphore, countdownlatch), thread safe set interview questions](/img/3a/b0bdf66e11e66234a222d977fd933c.png)
[multithreading] detailed explanation of JUC (callable interface, renntrantlock, semaphore, countdownlatch), thread safe set interview questions

Delegate parents and other loaders

Google Earth engine - Hansen global forest change v1.8 (2000-2020) forest coverage and forest loss data set

XSS.haozi.me刷题

Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记

委派雙親之類加載器

Today's sleep quality record 79 points
随机推荐
A fastandrobust volutionalneuralnetwork based defect detection model inproductqualitycontrol reading notes
Introduction to common distributed locks
Déléguer un chargeur tel qu'un parent
Unity3d read mpu9250 example source code
Unity高版本退回低版本报错问题
PowerCLI 脚本性能优化
Robot development -- robot data summary
Deep Learning for Generic Object Detection: A Survey-论文阅读笔记
Un modèle de détection par défaut basé sur le réseau neuronal évolutif rapide dans le contrôle de la qualité des produits - lire les notes
Use and principle of ThreadLocal variable
(1) Learn about MySQL
A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
The difference between journal log and oplog log
8.固定收益投资
Discussion on Euler angle solution of rocket large maneuvering motion
热议:老公今年已经34周岁想读博,以后做科研,怎么办?
Similarities and differences between OA system and MES system
A current List of AWESOME Qt and qml
Microservice online specification