Three-way leetcode on the subject.
Single number
Given an array of integers, every element appears twice except for one. Find the single one.
Test instructions namely: an array, the elements inside each appear two times, in addition to a special, to find this special element. Coder, who have been exposed to such topics, will soon be able to blurt out: Direct XOR or OK! The result is 0 because two of the same number is different or offsets each bit bit.
1 classSolution {2 Public:3 intSinglenumber (vector<int>&nums) {4 intx=0, len=nums.size ();5 for(intI=0; i<len;i++) x^=Nums[i];6 returnx;7 }8};
Single number II
Given an array of integers, every element appears three times except for one. Find the single one.
Test instructions: This is the enhanced version of the above topic, each element in the array appears three times, except for a special one, to find out this particular element.
Train of thought: We can also calculate the number of 1 bit by bitwise arithmetic, if each number appears three times, then the number of 1 bit on each bit is definitely a multiple of 3, the opposite is the special number if it is not a multiple of 3. But we can use a number of "auxiliary", because each bit of the 1 bit count is similar, so assume that a bit of the number of 1 bit is being counted. We use a
to represent the number of 1 bit, when the number of 1 bit is 0 o'clock, a=0; When the number is 1 o'clock, a=1; When the number is 2 o'clock, a=2? Non-Also, bit operations can only represent 0 and 1, so then we introduce the second variable B, we use b=1 to represent already have 2 1 bit, so when there are two 1 bit, a=0,b=1. The number of statistical results is 3 to 0, so only 0, 1, 23 kinds of results:
number of bits a b0 0 0 1 1 0 2 0 1
The idea is also obvious, each operation we maintain the values of A and B, the operation to the end can be obtained results:
1 classSolution {2 Public:3 intSinglenumber (vector<int>&nums) {4 intA=0, b=0, len=nums.size ();5 for(intI=0; i<len;i++){6b=a& (B^nums[i]);//b The corresponding bit of a and B XOR item and the decision7a=b| (A^nums[i]);//a corresponding bit or decision of B and a XOR or item8 }9 returnA;Ten } One};
Single number III
Given An array of numbers nums
, in which exactly-elements appear only once and all 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]
.
Test instructions: Or an array, each element appears two times, only two special elements appear once, the two special elements to find out.
Train of thought: direct XOR or no line. Consider two special values, they must have a bit different, find a way to separate them, and then separate with other numbers or, out. Divide all the numbers or (the effect is equivalent to the two special numbers XOR), take the result of a bit on the 1, according to this bit to divide the array into two sub-arrays, the purpose is achieved.
1 classSolution {2 Public:3vector<int> Singlenumber (vector<int>&nums) {4 intx=0, len=nums.size ();5 for(intI=0; i<len;i++) x^=Nums[i];6x=x& (-X);//take x rightmost bit of 17 intr1=0, r2=0;8 for(intI=0; i<len;i++) (nums[i]&x)? (R1^=nums[i]):(r2^=nums[i]);9vector<int>T;Ten T.push_back (R1); One T.push_back (R2); A returnT; - } -};
Reference: http://www.cnblogs.com/zichi/p/4795049.html
Wonderful bit arithmetic.