1. Question 1: In an array of the int type, only one number appears once, and the other number appears twice.
[Analysis] All exclusive or operations.
2. Question 2: In an array of the int type, except for two numbers, each number appears twice.
Reference: http://zhedahht.blog.163.com/blog/static/2541117420071128950682/
[Analysis]: The number of all or subsequent values is resultexclusiveor. Find the first digit of resultexclusiveor, and divide the array into two groups based on whether the value is 0.
/*** Creation Time: September 6, 2014 7:51:17 Project name: Test ** @ author Cao yanfeng * @ since JDK 1.6.0 _ 21 class description: only two arrays appear once, the other two appear twice, and the two numbers appear only once */Public classfindnumsappearoncetest {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub int [] array = {1, 2, 1, 9, 3, 2, 0, 3}; printnumsappearonce (array );} /* algorithm idea: the number of all or subsequent values is resultexclusiveor, locate its first bit instead of 0, and then divide the array into two groups based on whether this bit is 0, all in the group or all */public static void printnumsappearonce (INT [] array) {If (array = NULL | array. length <2) {return;} int resultexclusiveor = 0; For (INT I = 0; I <array. length; I ++) {resultexclusiveor ^ = array [I];} int mask = 1;/* as long as the phase is equal to or equal to 0, one digit is shifted to the left, until the template */while (resultexclusiveor & Mask) = 0) {mask <= 1 ;}int result1 = 0, result2 = 0; For (INT I = 0; I <array. length; I ++) {If (array [I] & Mask) = 0) {result1 ^ = array [I];} else {result2 ^ = array [I];} system. out. println ("the two numbers are" + result1 + "," + result2 );}}
3. Question 3: In an array of the int type, except for three numbers, all other numbers appear twice.
Another simple form of question: calculate any number of the three numbers.
Reference: http://zhedahht.blog.163.com/blog/static/25411174201283084246412/
[Analysis] uses an array as an example. The three numbers are different, for example, 001,010,100
1) The first traversal obtains the variance or X = a ^ B ^ C, that is, x = 111, which proves that X is different from A, B, and C;
2) x ^ A = B ^ c = 110, x ^ B = a ^ c = 101, x ^ c = a ^ B = 011, the three numbers are also different. The defined function f (x ^ A) = F (B ^ c) = 010 indicates that all bits except the last bit to 1 are reserved and set to 0, f (x ^ B) = 001, f (x ^ c) = 001. The result of the second traversal to obtain f (x ^ A) ^ f (x ^ B) ^ f (x ^ C) is f (x ^ A) ^ f (x ^ B) ^ f (x ^ c) = 010.
3) obtain the f {f (x ^ A) ^ f (x ^ B) ^ f (x ^ c)} result f {f (x ^) ^ f (x ^ B) ^ f (x ^ c)} = 010, that is, all other bit bits are set to 0, except for the position where the last bit is 1, note: here we get a template, f (x ^ A), F (x ^ B), F (x ^ C) that is, the numbers 010, 001, and 001 are only one bit of f (x ^.
4) for the third traversal, A can be obtained as long as all the numbers that match f (x ^ array [I]) = 010 are different or.
5) for the fourth traversal, find the last number exchange between A and the array, 0 ~ An array. The other two numbers can be obtained within the range of lenth-2.
/*** Creation Time: 9:36:17, January 1, September 6, 2014 * Project name: test * @ authorcao yanfeng * @ sincejdk 1.6.0 _ 21 * class description: only three arrays appear once, each Other appears twice, and the three numbers that appear only once * must be traversed at least 5 times. One of them is to delete the first element found or add another one, here it is deleted by exchanging it with the last element. */Public class findthreeuniquetest {/*** @ paramargs */public static void main (string [] ARGs) {// todo auto-generated method stub int [] array = {2, 3, 4, 5, 5, 0}; printthreeunique (array);} public static void printthreeunique (INT [] array) {If (array = NULL | array. length <3) {return;}/* for the first traversal, find the three numbers or x */INT xorresult = 0; For (INT I = 0; I <array. length; I ++) {xorresult ^ = array [I];}/* Second traversal, find f (x ^ A) ^ f (x ^ B) ^ f (x ^ c) Result */INT Index = 0; For (INT I = 0; I <array. length; I ++) {index ^ = lastbitof1 (xorresult ^ array [I]);}/* f (x ^ A) ^ f (x ^ B) ^ f (x ^ c) results are retained only for the last 1 bit, and the rest are set to 0. Assume that this bit is M bit and f (x ^) the M bit of is 1 */Index = lastbitof1 (INDEX);/* The third traversal, A must be in line with f (x ^) for (INT I = 0; I <array. length; I ++) {If (lastbitof1 (xorresult ^ array [I]) = index) {First ^ = array [I] ;}} system. out. println ("the first number is:" + first);/* The fourth Traversal, Locate the location of a and exchange it with the last number */For (INT I = 0; I <array. length; I ++) {If (array [I] = first) {array [I] ^ = array [array. length-1]; array [array. length-1] ^ = array [I]; array [I] ^ = array [array. length-1];}/* between 0 and ~ Array. find two numbers in the range of length-2 */printnumsappearonce (array, array. length-1);} public static void printnumsappearonce (INT [] array, int lenght) {If (array = NULL | lenght <2) {return ;} int resultexclusiveor = 0; For (INT I = 0; I <lenght; I ++) {resultexclusiveor ^ = array [I];} int mask = 1; /* as long as the phase is & equal to 0, move one digit to the left until the template */while (resultexclusiveor & Mask) = 0) {mask <= 1;} is obtained ;} int result1 = 0, result2 = 0; For (int I = 0; I <lenght; I ++) {If (array [I] & Mask) = 0) {result1 ^ = array [I];} else {result2 ^ = array [I];} system. out. println ("the second and third numbers are:" + result1 + "," + result2);}/* only retain the last digit 1, other BITs are set to 0 */public static int lastbitof1 (INT Arg) {return Arg &~ (Arg-1 );}}
4. Question 4: In an array of the int type, all the other values appear once or twice.
Reference: http://blog.csdn.net/dinosoft/article/details/6443354
[Analysis] for each bit, this bit is cleared once it appears three times.
Analyze the following code:
/*** Creation Time: 10:25:21, January 1, September 6, 2014 * Project name: test * @ author Cao yanfeng * @ since JDK 1.6.0 _ 21 * class description: in an array of the int type, except for a number that appears twice, all other numbers appear three times. */Public classfinddoubletest {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub int [] array1 = {1, 1, 20}; printdouble (array1, 1); int [] array2 = {1, 1, 20, 20 }; printdouble (array2, 2);} public static void printdouble (INT [] array, Int oneortwo) {int ones = 0, twos = 0; For (INT I = 0; I <array. length; I ++) {twos | = ones & array [I]; ones ^ = array [I]; int not_threes = ~ (Ones & twos); ones & = not_threes; twos & = not_threes;} switch (oneortwo) {Case 1: system. out. println ("number of occurrences 1:" + ones); break; Case 2: system. out. println ("number of occurrences:" + twos); break; default: break ;}}}
If the first three items of the array are the same number A, the number before ones & = not_threes and twos & = not_threes is displayed in front of the arrow, the arrow is followed by the number after ones & = not_threes and twos & = not_threes ,.
I = 0 |
I = 1 |
I = 2 |
Twos = 0 → 0 |
Twos = A → |
Twos = A → 0 |
Ones = A → |
Ones = 0 → 0 |
Ones = A → 0 |
Not_threes = 1 |
Not_threes = 1 |
Not_threes = ~ A |
According to this analysis, when a appears, ones can save. When a appears twice, twos can save. When a appears three times, both ones and twos are cleared. Therefore, if all the numbers in a value are passed through this loop, the three numbers are cleared. If one number exists once, it is stored in ones; if it appears twice, it is saved in twos.
Summary of the number of queries in an int Array