Topic: In an integer array, except for two digits, the other numbers appear two times.
* Please use some procedures to find out the two numbers that appear only once. Required time complexity O (n), Space complexity O (1)
For example, input array {2,4,3,6,3,2,5,5}, because only 4, 6 of these two digits appear only once, the other numbers appear two times, so the output 4,6
This is a relatively difficult topic, very few people in the interview do not need to be prompted to think of the best solution. Generally when the candidate thought for a few minutes after that there is no thought, the interviewer will give some hints.
We think of a property of XOR or operation: Any number that is different or it is equal to 0, that is, if we order from beginning to end or each number in the array, the final result is exactly which one appears, because the number of those two times that appear in pairs is lower in the XOR.
We tried to divide the array into two sub arrays, so that each child array contained only one occurrence of the number, while the other numbers appeared in pairs two times. If we can split this into two arrays, we can figure out two numbers that appear only once, according to the previous approach.
We are either from the beginning or the end of each number in the array, then the resulting result is the result of two occurrences of only one occurrence of the number. Because all the other numbers appear two times, they all cancel out in different or. Since these two numbers are certainly different, the difference or result is certainly not 0, which means that at least one of the binary representations of the resulting number is 1. We find the position of the first digit 1 in the result number, which is the nth digit. Now we're going to divide the numbers in the original array into two sub arrays using the nth 1 as the standard, and the nth digit of each number in the first sub array is 1. The first n bits of each number in the second child array are 0. Since we are grouping the criteria for whether one of the numbers is 1 or 0, the number that appears two times is definitely assigned to the same child array. Because any one of the two identical numbers is the same, we could not allocate two identical numbers to two sub arrays, so we have divided the original array into two sub arrays, each containing a single occurrence of the number, while the other numbers appear two times. We already know how to find the only number that appears once in the array, so all the problems are resolved at this point.
For example, the number we entered is {2,4,3,6,3,2,5,5}. When we sequence each number in an array, the resulting result is expressed in binary notation as 0010. The penultimate digit of the result is 1, so we are divided into two arrays based on the penultimate digit of the number. All numbers in the first child array {2,3,6,3,2} are the penultimate digit of 1. The 2nd digit of all numbers in the second sub-array {4,5,5} is 0, so you can find out that only one occurrence of the first child array is 6, as long as each of the two sub arrays is evaluated separately. The number that appears only once for the second child array is 4.
The Java implementation code is as follows:
/** * An integer array with the exception of two digits, the other numbers appear two times. * Please use some procedures to find out the two numbers that appear only once.
Require time complexity O (n), Space complexity O (1) * * * Package swordforoffer; /** * @author Jinshuangqi * * * August 10, 2015/public class E40numbersappearonce {public void findnumsappearonce (int[
] {if (arr = null) return arr;
int number = 0;
for (int i:arr) number^=i;
int index = FINDFIRSTBITIS1 (number);
int number1= 0,number2 = 0;
for (int i:arr) {if (IsBit1 (I,index)) number1^=i;
else number2^=i;
} System.out.println (Number1);
System.out.println (NUMBER2);
private int findFirstBitIs1 (int number) {int indexbit = 0;
while ((number & 1) = = 0) {Number = number >> 1;
++indexbit;
return indexbit;
Private Boolean isBit1 (int number,int index) {number = number >>index;
Return (number & 1) = = 0;
public static void Main (string[] args) {int[] arr={6,2,4,3,3,2,5,5};
E40numbersappearonce test = new E40numbersappearonce ();
Test.findnumsappearonce (arr);
}
}