Single number III

Source: Internet
Author: User

Original: Given An array of numbers nums,inchWhich exactly, elements appear only once and all of the other elements appear exactly twice . Find the elements that appear only once. For Example:given Nums= [1,2,1,3,2,5],return[3,5]. Note:the Order of the result isNot important. SoinchThe above example, [5,3] isalso correct. Your algorithm should runinchLinear runtime complexity. Could you implement itusingOnly constant space complexity?credits:special thanks to @jianchao. Li.fighter forAdding Thisproblem and creating all test cases. Subscribe to see which companies asked Thisquestion Simple point, that is, an array of two different numbers are not in pairs, the other numbers are in pairs appear

 Public int[] Singlenumber (int[] nums) {        intMID = 0, index = 0, left = 0, right = 0; //computes the XOR value of all the numbers in the array, as in mid         for(inti = 0, Len = nums.length;i < Len;i + +) {Mid= Mid ^Nums[i]; }        //find the lowest bit in mid 1 is the number, which is recorded as index         while((Mid & (1 << index)) = = 0) {Index++; }                        //depending on the number of index bits in the array is 1 or 0 different, divided into two parts, and these two parts have exactly two different numbers, the rest of the numbers are paired up         for(inti = 0, Len = nums.length;i < Len;i + +){            if((Nums[i] & (1 << index)) = = 0) { left= left ^Nums[i]; }Else{ Right= right ^Nums[i]; }        }        return New int[]{left, right}; }

Reference:

http://blog.csdn.net/morewindows/article/details/8214003

Single number III

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.