当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

本周四晚19:00知识赋能第六期第5课丨OpenHarmony WiFi子系统
![[Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory](/img/a1/b1c04d44ea74560a0be55a3b715d25.png)
[Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory

TRCX:掺杂过程分析

MySQL indexes and transactions

This Thursday evening at 19:00, Lesson 5 of the sixth phase of knowledge empowerment丨OpenHarmony WiFi subsystem

Detailed explanation of common methods of filtering matrix (array) elements in Matlab

Oops Framework模板项目新手引导

惨遭面试官吊打高并发系统设计,回来学习 2400 小时后成功复仇

2022年PMP报考指南

络达开发---串口日志&日志过滤
随机推荐
软件测试面试题:Web服务器指标指标?
两日总结十
gRPC闭包调度器
软件测试面试题:缺陷等级应如何划分?
Summary of DDL routine operations in MySQL
The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released
Fatal error in launcher: Unable to create process using xxx --logdir logs(tensorboard使用)
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
C# WebBrower1控件可编辑模式保存时会提示“该文档已被修改,是否保存修改结果”
【HFSS学习记录1】实例:宽带非对称多节定向耦合器设计
如何开展性能测试,你知道吗?
OpenWrt之opkg详解
软件测试面试题:单元测试的策略有哪些?
测试3年,开口就要25k?面试完最多给15k...
postgresql parameter meaning
The statistical data analysis, interview manual"
隐私计算融合应用研究
13.cuBLAS开发指南中文版--cuBLAS中的Level-1函数copy()和dot()
Experiment record of Shengxin (part3)--scipy.spatial.distance_matrix
How to realize the repeatable design of FPGA