力扣今日题-565. 数组嵌套
作者:互联网
565. 数组嵌套
思路:
class Solution {
public int arrayNesting(int[] n) {
int numsLength = n.length;
boolean[] visited = new boolean[numsLength];
int res= 1;
for(int i = 0 ; i < numsLength; i++){
if(res > numsLength/2){
return res;
}
if(visited[n[i]]){
continue;
}
int curMax = 1;
int cur = n[n[i]];
while(n[i] != cur){
visited[cur] = true;
curMax++;
cur = n[cur];
}
res = curMax > res? curMax : res;
}
return res;
}
}
标签:cur,numsLength,int,res,curMax,565,力扣,嵌套,visited 来源: https://www.cnblogs.com/liyublogs/p/16487992.html