当前位置:网站首页>The sixth day of brushing questions with force deduction
The sixth day of brushing questions with force deduction
2022-07-25 03:27:00 【Xiao Tang Xuejie】
Index from 0 Start length is N Array of A, contain 0 To N - 1 All integers of . Find the largest set S And return its size , among S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } And abide by the following rules .
Suppose the selection index is i The elements of A[i] by S The first element of ,S The next element of should be A[A[i]], And then A[A[A[i]]]... And so on , Keep adding until S Duplicate elements appear .
Example 1:
Input : A = [5,4,0,3,1,6,2]
Output : 4
explain :
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.
One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}
class Solution {
public int arrayNesting(int[] nums) {
// Define a longest number as the return
int longnum = 0;
// Loop through the ring corresponding to each subscript
for(int i = 0; i < nums.length; i++){
int cont = i;
int sum = 0;
// Mark the accessed elements as -1 after , The starting element of the next ring is if -1
// That means the formed ring is formed before , There is no need to calculate , Just counting is not -1 Of
while(nums[cont] != -1){
sum++;
int next = nums[cont];
// take -1 As a mark , The accessed element is changed to -1
nums[cont] = -1;
cont = next;
}
if(longnum <= sum){
longnum = sum;
}
}
return longnum;
}
}边栏推荐
- C language writes a circular advertising lantern or changes it to a confession system
- Image processing based on hog feature
- VMware installation
- Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering
- Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
- Record once C # extract audio files with ffmempeg
- Bgy development small example
- Optimization of MySQL sorting index fields
- Easyexcel sets the style of the last row [which can be expanded to each row]
- 10. 509 Certificate (structure + principle)
猜你喜欢

How to use two queues to simulate the implementation of a stack

Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection

NVM installation and use

Use and introduction of vim file editor

Li Kou 279 complete square - dynamic programming
![Matplotlib tutorial (I) [getting to know Matplotlib first]](/img/dc/e7dfc435900c14e3e9be07f6ad75fe.png)
Matplotlib tutorial (I) [getting to know Matplotlib first]

Database transactions (often asked)

Chrome process architecture

What is technical support| Daily anecdotes

What should testers do if they encounter a bug that is difficult to reproduce?
随机推荐
Recursive and non recursive methods are used to realize the first order, middle order and second order traversal of binary tree respectively
Secondary vocational network security skills competition P100 web penetration test
mysql_ User table_ Field meaning
Openlayers ol ext: Transform object, rotate, stretch, zoom in
10. 509 Certificate (structure + principle)
From input URL to page presentation
Performance test indicators using JMeter
Use of stm32cubemonitor part I - data plotting and instrument display
What is technical support| Daily anecdotes
Message queue (MQ)
Visio use
TypeScript
Function method encapsulation -- mutual conversion of image types qpixmap, qimage and mat
Network construction and application in 2020 -- the answer of samba in Guosai
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
Unified return data format
[golang] golang realizes sending wechat service number template messages
Database transactions (often asked)
Time complexity and space complexity
Reasons for not sending requests after uni app packaging