In an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once. The time complexity is O (n) and the space complexity is O (1)

Source: Internet
Author: User

Question: except two numbers in an integer array, the other numbers appear twice. Write a program to find the numbers that appear only once. The time complexity is O (n) and the space complexity is O (1 ).

Idea: The question requires that the time complexity be O (N) and the space complexity be O (1). This means that the comparison-based solution cannot be completed, and this question requires an exclusive or nature, exclusive or (0, 1, not carry addition)

Because the other numbers appear twice, after a scan is completed, all the numbers are set to an exclusive OR, and the same number is changed to 0, and the result is an exclusive or of two different numbers. For example, if, or is an exclusive or, or 0, the result is equivalent to 9 or 2.

Here there are two numbers. Suppose there is only one number, so we only need to scan it once, and all the numbers are different or we can get the result. What if there are two numbers? The idea is to divide the two numbers into two different groups.

What is the basis for division?

1. In this case, we can first merge all the numbers. If the result is 11001100, we can use the lowest one bit (that is, 2nd bits, low to high, from 0) [In essence, it can be divided by any one, but this is the most convenient]. Why can one be divided into two different numbers? When two numbers are written in binary form, the uniqueness or result shows one bit (two different numbers at the same time are different or there must be one, because the same number must be 0 ), they are different from each other. (The difference is 1, and the same is 0). Therefore, when dividing by 1, different two numbers are bound to two different groups.

2. In the second step, the two sub-arrays are converted into an exclusive or result, because the same number will get 0.

3. Tips: Determine whether the bit is 1 in the middle. The method is: Pass the bit to be judged first.Right ShiftMove to the last position, and then do it with 0000... 00001Bitwise AND,If the last bit is 1, the result is 1. If the last bit is 0, the result is 0.

Code:

/* Except two numbers in an integer array, the other numbers appear twice. Write a program to find the numbers that appear only once. The required time complexity is O (n), and the space complexity is O (1) */# include <iostream> using namespace STD; int findfirstbitis1 (INT sum) // find the index number {int Index = 0; while (1 & sum) = 0) where the first position is 1 (right to left) // and 000 .. 00001. If the last digit is 0, it is equal to 0. If the last digit is 1, it is 1 {sum = sum> 1; ++ index;} Return Index ;} int judgeindexbitis1 (INT num, int index) {num = num> index; // shifts the index bit to the right, so that the first digit to be judged is in the last position return (1 & num ); // and 000 .. 0001 and, 1 returns 1, 0 returns 0} void XOR (int * a, int Len) {int sum = 0; // The number of two-phase differences for (INT I = 0; I <Len; ++ I) {sum = sum ^ A [I];} int Index = findfirstbitis1 (SUM); int num1 = 0, num2 = 0; // Save the result for (INT I = 0; I <Len; ++ I) {If (judgeindexbitis1 (A [I], index )) num1 = num1 ^ A [I]; else num2 = num2 ^ A [I];} cout <num1 <"" <num2 <Endl;} void main () {int A [] = {9, 0,}; int Len = sizeof (a)/sizeof (A [0]); XOR (, len );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.