当前位置:网站首页>0 in the figure, etc. LeetCode565. Array nesting
0 in the figure, etc. LeetCode565. Array nesting
2022-08-11 01:56:00 【18 Aru】
描述
索引从0开始长度为N的数组A,包含0到N - 1的所有整数.找到最大的集合S并返回其大小,其中 S[i] = {A[i], A[A[i]], A[A[A[i]]], … }且遵守以下的规则.
假设选择索引为i的元素A[i]为S的第一个元素,S的下一个元素应该是A[A[i]],之后是A[A[A[i]]]… 以此类推,不断添加直到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}
提示:
1 <= nums.length <= 105
0 <= nums[i] < nums.length
A中不含有重复的元素.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/array-nesting
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
分析
从一个点出发,Will definitely go back to this point again,即构成环;
数值不重复,Explain that the ring does not intersect the ring;
Find the largest ring;
class Solution {
public int arrayNesting(int[] nums) {
int ans = 0;
boolean[] visited = new boolean[nums.length];
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (visited[i]) {
continue;
}
int size = 1;
visited[i] = true;
while (!visited[num]) {
size++;
visited[num] = true;
num = nums[num];
}
ans = Math.max(size,ans);
}
return ans;
}
}
边栏推荐
- [Server data recovery] Data recovery case of lvm information and VXFS file system corruption caused by raid5 crash
- 联盛德W801系列6-从微信小程序的角度来分析W801的蓝牙通信源码(indicate方式)
- 88Q2110 通过C22方式访问C45 phy地址
- 3342:字符串操作 题解
- 【开源】壁纸软件,给自己电脑设计专属特效
- The concept of services
- Summarize the acquisition of commonly used file information QFileInfo in Qt: suffix, name, path, link
- 研发项目流程规范
- Update chromedriver driver programming skills │ selenium
- 22. Inventory service
猜你喜欢

测试3年,开口就要25k?面试完最多给15k...

联盛德W801系列5-微信小程序与W801蓝牙通信例程(阅读笔记)
![Deep Learning [Chapter 2]](/img/09/5eb16731c3c47965da131c2aa0c2c3.png)
Deep Learning [Chapter 2]

FPGA学习专栏-串口通信(xinlinx)

请讲一讲JS中的 for...in 与 for...of (下)
![【iframe父页面调用子页面的方法】踩坑:获取元素的时候需要用 `[x]`是关键,不能用`.eq(x)`否则获取不到。](/img/ec/0cca8c7011770429c34a6aa1f36460.png)
【iframe父页面调用子页面的方法】踩坑:获取元素的时候需要用 `[x]`是关键,不能用`.eq(x)`否则获取不到。

漏洞管理计划的未来趋势

postgresql parameter meaning
![[ASM] The relationship between the role of the bytecode operation ClassWriter COMPUTE_FRAMES and visitMaxs](/img/28/66370d46ebeb1e16b56ea2a36fe100.jpg)
[ASM] The relationship between the role of the bytecode operation ClassWriter COMPUTE_FRAMES and visitMaxs
![报错处理:org.xml.sax.SAXParseException: 不允许有匹配 “[xX][mM][lL]“ 的处理指令目标](/img/35/650c92ac4c5fc2d5826f3216a09e65.png)
报错处理:org.xml.sax.SAXParseException: 不允许有匹配 “[xX][mM][lL]“ 的处理指令目标
随机推荐
最新国产电源厂家及具体型号pin-to-pin替代手册发布
联盛德W801系列5-微信小程序与W801蓝牙通信例程(阅读笔记)
【Video】Report Sharing | 2021 Insurance Industry Digital Insights
Construction inspection, no rules and no square
Shengxin experiment record (part2)--tf.reduce_sum() usage introduction
js原型和原型链及原型继承
async和await的理解和用法
基于 HPSO 与多核 LSSVM 的网络入侵检测
gRPC基础概念:闭包
paddle2.3和torch1.8在SentenceBert上的性能对比
软件测试面试题:什么是α测试,β测试?
ora-00001违反唯一约束
软件测试面试题:软件测试的过程的V模型,说出它的缺点?
FPGA学习专栏-串口通信(xinlinx)
英伟达 GPU 架构简史
C# string与stream的相互转换
连流量染色都没有,你说要搞微服务?
Please talk about for...in and for...of in JS (below)
【websocket】
MySQL - 一条SQL在MySQL中是如何被执行的?