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