Single number I:
Given an array of integers, every element appears twice except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Solution:
A number of solutions, paste a:
1 classSolution:2 #@param {integer[]} nums3 #@return {integer}4 defSinglenumber (Self, nums):5Ans =Nums[0];6 forIinchRange (1, Len (nums)):7Ans ^=Nums[i]8 returnAns
It is based on:
1, the different or the operation satisfies the Exchange law;
2, a ^ a = 0;
3, b ^ 0 = b.
The key to this problem lies in the linear time of the same pair of "elimination", leaving the "single".
An XOR operation gives such a quick possibility.
Single number II:
Given an array of integers, every element appears three times except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Solution:
Again, consider using bit arithmetic to solve problems:
The problem of the "elimination" of duplicate elements, leaving only the elements of "falling", is cleverly solved by using XOR or arithmetic in I. In II, in addition to the element to be found, each element appears 3 times, I solution for the occurrence of an even number of cases, but for the odd number of times no longer applicable.
Consider each number in binary expansion, comparing the numbers horizontally:
For a 32-bit (or 64-bit) integer, for a bit of these 32 bits, if each number appears three times, then for all the number on this bit "1" number, must be a multiple of 3, and conversely, if there is a number is not 3 times (nor 3 times, this is the topic is not said Housetops, I The same is true for this), then for a bit of 1 after its binary expansion, the number of "1" for all numbers on this bit must not be a multiple of 3.
So the specific solution is:
With a 32-long array storage for all numbers of the binary expansion, each bit on the total number of "1" and finally see those bits on the "1" number is not a multiple of 3, then this one in ANS is 1.
The algorithm is O (32n).
One thing to be aware of is:
The processing of bit operations in different languages, especially the sign bit, is not the same, for example, the highest level in C + + is the sign bit, if it is not a multiple of 3 then the last ans is negative, the sign bit can be treated like other bits, but if it is python, because of its dynamic type of characteristics, when the data range, is automatically converted to a larger range of data types without being treated as a symbol bit.
C + + version:
1 classSolution {2 Public:3 intSinglenumber (intA[],intN) {4 intbitnum[ +] = {0};5 intres=0;6 for(inti =0; I < +; i++){7 for(intj =0; J < N; J + +){8Bitnum[i] + = (A[j] >> i) &1;9 }TenRes |= (Bitnum[i]%3) <<i; One } A returnRes; - } -};
Python version:
1 classSolution:2 #@param {integer[]} nums3 #@return {integer}4 defSinglenumber (Self, nums):5Bitnum = [0] * 326 forIinchRange (32):7 forEinchNums:8Bitnum[i] + = (e >> i) & 19Ans =0Ten forI, ValinchEnumerate (bitnum): One ifi = = 31 andVal% 3! =0: AAns =-((1 << i)-ans) - Else: -Ans |= (val% 3) <<I the returnAns
Where the logical sign bit (32nd bit) is judged and processed individually, if 1, it is required to convert to the corresponding negative number (the absolute value of the corresponding negative number = "modulo"-a positive number regardless of the sign bit (sign bit 0)).
"Leetcode" single number I & II