Topic 1number of falling orders
Given the number of 2*n + 1, each number except one of the numbers appears two times, to find this number.
Links: http://www.lintcode.com/zh-cn/problem/single-number/
Sample Example
Given [1,2,2,1,3,4,3], return 4
challenges
The extra space complexity of a single traversal, constant number of levels
Solution Solutions
Method 1: Convert all the numbers to binary because they are of type int, total 32 bits. Apply the extra space for the constant level (32-bit), then add the bits corresponding to each number, and finally correspond to the sum of the digits on the bit 2. The final result is a binary number corresponding to a single number.
classSolution { Public: /** * @param a:array of integers. * Return:the single number. */ intSinglenumber (vector<int> &A) {//Write your code here intans[ *]; memset (ans,0,sizeof(ans)); for(intI=0; I<a.size (); ++i) { for(intk=0; k< +; k++) Ans[k]= (ans[k]+ (a[i]>>k) &1))%2; } intRET =0; int Base=1; for(intI=0; i< +; ++i) {ret+ = ans[i]*Base; Base*=2; } returnret; }};
Method 2 idea: Through XOR, the same number result is 0, then the final result must be the number of the single.
classSolution { Public: /** * @param a:array of integers. * Return:the single number. */ intSinglenumber (vector<int> &A) {//Write your code here intAns =0; for(intI=0; I<a.size (); ++i) ans^=A[i]; returnans; }};
Topic 2number of falling ordersII
Given the number of 3*n + 1, each number except one of the numbers appears three times, to find this number.
Links: http://www.lintcode.com/zh-cn/problem/single-number-ii/
Sample Example
Given [1,1,2,3,3,3,2,2,4,1] , return 4
challenges
The extra space complexity of a single traversal, constant number of levels
Solution Solutions
The method of the previous question 1 the same way of thinking.
classSolution { Public: /** * @param a:an integer Array * @return: an integer*/ intSinglenumberii (vector<int> &A) {//Write your code here intans[ *]; memset (ans,0,sizeof(ans)); for(intI=0; I<a.size (); ++i) { for(intk=0; k< +; k++) Ans[k]= (ans[k]+ (a[i]>>k) &1))%3; } intRET =0; int Base=1; for(intI=0; i< +; ++i) {ret+ = ans[i]*Base; Base*=2; } returnret; }};
Topic 3: The number of orders fallingIII
The number of 2*n + 2 is given, except two of which each number appears two times, and the two numbers are found.
Links: http://www.lintcode.com/zh-cn/problem/single-number-iii/
Sample Example
Given [1,2,2,3,4,4,5,3], return 1 and 5
challenges
O (n) time complexity, O (1) Additional space complexity
Solution Solutions
As shown, the result of the XOR of all the numbers equals the result of two singular or odd (set to s). How can we find out the number of orders based on this XOR result? First of all, the value of s must not be 0, then find the binary value of s corresponding to 1 bits (find any bit 1 is the line, here we find the binary of S the last is a bit of 1, set to P), according to this position, all the number is divided into two parts, part of the corresponding binary P-bit is 1, The other part corresponds to the binary P-bit is 0. This divides the number of two orders into different sets . such as the Red Box collection and the Green Box collection. Then converted to "2*m+1 number, in addition to a number of other numbers appear two times" problem, that is, topic 1: The number of single-drop I.
classSolution { Public: /** * @param a:an integer array * @return: integers*/ intFindNum (intK, vector<int> &a,BOOLflag) { intRET =0; for(intI=0; I<a.size (); ++i) { if(Flag && (1<<K) &A[i]) ret^=A[i]; if(!flag &&!) ((1<<K) &A[i])) RET^=A[i]; } returnret; } Vector<int> Singlenumberiii (vector<int> &A) {//Write your code here intx =0; for(intI=0; I<a.size (); ++i) x^=A[i]; intK =0; for(k; k< +; ++K)//Find the XOR value last 1, indicating that the position p, two different numbers corresponding to the binary is the same if((1<<K) &x) Break; //according to this position p, converted to "2*m+1 number, except for a number of the other numbers appear two times" problem//The number corresponding to 1 on position p is different or a number is obtained, and then the number corresponding to 0 on position p is different or a number is obtained, and finally the answer is obtained.vector<int>ans; Ans.push_back (FindNum (k, A,true)); Ans.push_back (FindNum (k, A,false)); returnans; }};
Number of Lintcode (bit operation)