"Sword means offer" 3, repeated numbers in the array

Source: Internet
Author: User

Title One: Find the duplicate numbers in the array.

。。。。。。 S2

。。。。。

1 classSolution {2  Public:3     //Parameters:4     //numbers:an array of integers5     //length:the length of array numbers6     //duplication: (Output) The duplicated number in the array number7     //Return Value:true If the input is valid, and there be some duplications in the array number8     //otherwise false9     BOOLDuplicateintNumbers[],intLengthint*duplication) {Ten         if(numbers = = nullptr | | length <=0){ One             return false; A         } -          -          the          for(inti =0; I < length;i++){ -             if(Numbers[i] <0|| Numbers[i] >=length) { -                 return false; -             } +         } -          +              A          for(intI=0; i < length; i++){ at              while(Numbers[i]! =i) { -                 if(Numbers[i] = =Numbers[numbers[i]]) { -*duplication =Numbers[i]; -                     return true; -                 } -                 inttemp; intemp =Numbers[numbers[i]]; -Numbers[numbers[i]] =Numbers[i]; toNumbers[i] =temp; +             } -         } the         return false; *     } $};

"Sword means offer" 3, repeated numbers in the array

Related Article

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.