The value range is 0 to 1001 ~ 1000 of the numbers to find all the repeated numbers. No additional space is needed on the basis of O (n ).

Source: Internet
Author: User
I saw a question. "The 1001 ranges from 1 ~ 1000 of the number, find a repeated number, O (n) based on no additional space "someone gives the solution: int I = 0; Int T = 0; While (A [I]! = 0 ){ T = A [I]; A [I] = 0; I = T; } System. Out. println (t); in addition, to meet the "no extra space required" requirement, remove t to this: int I = 0; while (A [I]! = 0) I = A [I] + (A [I] = 0); system. Out. println (I); yes, this solution can solve the problem. However, to increase the difficulty, if I change the number range to 0 ~ 1000, this AlgorithmNo, even if you change the algorithm to int I = 0; while (A [I]! =-1) I = A [I] + 1 + (A [I] =-1); system. out. println (I); // output I; When a [0] = 0, the problem cannot be solved. If you change the algorithm to int I = 0; while (A [I]! =-1) I = A [I] + 1 + (A [I] =-1) + 1; system. out. println (I); // output I; the array subscript is out of bounds. To increase the difficulty, the new question is: the range of 1001 is 0 ~ 1000To find out AllThere is no extra space for repeated numbers based on O (N). Can you come up with a solution? I already have the solution. I will not announce it for the moment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.