[Link to this article]
Http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html
【Question]
Three numbers a, B, and c appear only once in an array, and the other numbers appear twice. Find three numbers that appear only once.
【Analysis]
This is a novelBitwise operation. Previous blog posts34. two numbers that appear only once in the array [Find two numbers which appear once]Analysis of N = 1 and N = 2.
(1). N = 1, the result of all numbers in the array is.
(2 ). when N = 2, all numbers in the array are exclusive or the result is equal to a ^ B. The array is divided into two groups based on the position where the last 1 in the binary of a ^ B; returns a and B if the numbers in each group are different or different.
(3) When. N = 3, all numbers in the array are exclusive or the result is equal to a ^ B ^ c. How can we differentiate them? If we can find a number that appears only once, the remaining two digits that appear only once can be converted to N = 2.
The specific ideas are as follows:
(1). f (x) = x & (-x) The result is the position of the last 1 of x.
(2). x = a ^ B ^ c.
(3 ). flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) the result must be 1, because f (m) ^ f (n) the result is 0 or 2.
(4 ). f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) where the m-bit is 1, then x ^ a, x ^ B, x ^ c must have 1 or 3 m-th digits as 1.
(5). We can use the reverse identification method to obtain that x ^ a, x ^ B, and x ^ c have only one m-bit as 1.
For example, data = {1, 2, 4, 4, 5, 5, 6}
X = a ^ B ^ c = 1 ^ 2 ^ 3 = 000
X ^ a = 001, x ^ B = 010, x ^ c = 011
F (x ^ a) = 001, f (x ^ B) = 010, f (x ^ c) = 001
Flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) = 010, flag = f (flag) = 010
F (x ^ B) = flag
First = B = 1
Second = 3, third = 1
The complete code is as follows:
【Code]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
// 58_FindNumbersAppearOnce.cpp: Defines the entry point for the console application. // /* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: 2014/5/27 */
# Include "stdafx. h"
// Find number which appear once Void Find1NumberAppearOnce (int data [], int length, int & num) { If (NULL = data | length <1) Return;
// Get the exclusive or result of array // Int xor = 0; For (int I = 0; I <length; ++ I) Xor ^ = data [I]; Num = xor; }
// Get last 1 bit of n // N = 00001110 ---> 00000010 Unsigned int GetLast1Bit (int n) { Return n & (-n ); }
// Find 2 numbers which appear once Void Find2NumbersAppearOnce (int data [], int length, int & num1, int & num2) { If (NULL = data | length <2) Return;
// Get the exclusive or result of array // A ^ B Int xor = 0; For (int I = 0; I <length; ++ I) Xor ^ = data [I];
// Find the last bit 1 of xor Int flag = GetLast1Bit (xor ); Num1 = num2 = 0; For (int j = 0; j <length; ++ j) { // Divide numbers in data into 2 groups by flag: // Numbers in group1: the & result is 1 // Numbers in group2: the & result is 0 If (flag & data [j]) { Num1 ^ = data [j]; } Else { Num2 ^ = data [j]; } } }
// Swap a and B Void myswap (int & a, int & B) { Int t =; A = B; B = t; }
// Find 3 numbers which appear once /* (1). f (x) = x & (-x) (2). x = a ^ B ^ c (3). flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) (4). flag = f (flag) (5). x ^ a, x ^ B, x ^ c, only one of three is 1 at m-bit F (x ^ a) = flag
For example: Data = {1, 2, 3, 4, 5, 5, 6} X = a ^ B ^ c = 1 ^ 2 ^ 3 = 000 X ^ a = 001, x ^ B = 010, x ^ c = 011 F (x ^ a) = 001, f (x ^ B) = 010, f (x ^ c) = 001 Flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) = 010 Flag = f (flag) = 010 F (x ^ B) = flag First = B = 1 Second = 3, third = 1 */ Void Find3NumbersAppearOnce (int data [], int length, int & num1, int & num2, int & num3) { If (NULL = data | length <3) Return;
// Get the exclusive or result of array // A ^ B ^ c Int xor = 0; For (int I = 0; I <length; I ++) Xor ^ = data [I];
Int flag = 0; For (int I = 0; I <length; I ++) Flag ^ = GetLast1Bit (xor ^ data [I]); Flag = GetLast1Bit (flag );
// Get the first unique number Int first = 0; For (int I = 0; I <length; I ++) If (GetLast1Bit (data [I] ^ xor) = flag) First ^ = data [I]; Num1 = first;
// Move the first number to the end of array For (int I = 0; I <length; I ++) { If (first = data [I]) { Myswap (data [I], data [length-1]); Break; } }
// Get the second and third unique number Find2NumbersAppearOnce (data, length-1, num2, num3 ); }
// ================================================ ====================================== // Test cases Void test_base1 (int data [], int length) { Int num1; Find1NumberAppearOnce (data, length, num1 ); Printf ("% d \ n", num1 ); }
Void test_base2 (int data [], int length) { Int num1, num2; Find2NumbersAppearOnce (data, length, num1, num2 ); Printf ("% d \ n", num1, num2 ); }
Void test_base3 (int data [], int length) { Int num1, num2, num3; Find3NumbersAppearOnce (data, length, num1, num2, num3 ); Printf ("% d \ n", num1, num2, num3 ); }
Void test_case1 () { Int data [] = {1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6 }; Int length = sizeof (data)/sizeof (int ); Test_base1 (data, length ); }
Void test_case2 () { Int data [] = {1, 2, 3, 3, 4, 4, 5, 5, 6, 6 }; Int length = sizeof (data)/sizeof (int ); Test_base2 (data, length ); }
Void test_case3 () { Int data [] = {1, 2, 3, 4, 4, 5, 5, 6, 6 }; Int length = sizeof (data)/sizeof (int ); Test_base3 (data, length ); }
Void test_main () { Test_case1 (); // 1 Test_case2 (); // 1 2 Test_case3 (); // 2 3 1 } // ================================================ ======================================
Int _ tmain (int argc, _ TCHAR * argv []) { Test_main (); Return 0; } |
[Reference]
Http://www.cnblogs.com/hellogiser/p/3738909.html
Http://zhedahht.blog.163.com/blog/static/25411174201283084246412/
Http://www.cnblogs.com/youxin/p/3349834.html
Http://www.cnblogs.com/kedebug/archive/2012/12/22/2829013.html
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html