The number of only one occurrence in the array of "offer of Swords" (1)

Source: Internet
Author: User

Reprint Please specify source:http://blog.csdn.net/ns_code/article/details/27649027


Topic Description Narration:
In
an integer array, except for two digits, the other numbers appear two times. Please tap the code to find the two numbers that appear only once.
Input:
Each test case consists of two lines: the first line includes an integer n, which indicates the size of the array. 2<=n <= 10^6. The second line includes n integers, which represent the array elements, and the elements are int.

Output:
For
each test case, the output array only appears once in two numbers. The output of the numbers from small to large order.
Example input:
82 4 3 6 3 2 5 5
Example output:
4 6

Idea: The principle of xor or de-weight has been learned in the previous blog post, and it is known that there is only one number that only appears once, but there are two numbers that appear only once. We have to find a way to divide him into two sub-arrays, each of which includes a number that appears only once, and the other numbers appear two times. Sword refers to the idea of an offer is very ingenious, still from beginning to end or all the numbers, so that the result is actually two only one occurrence of the number of different or results, we find in the results of the second binary in the second and the right is 1 bit, the bit since 1, the corresponding two digits of the same bit must be different , one must be 1 and one is 0, so we can consider whether this bit is the first to divide the two sub-arrays. So the two only occurrences of the number is separated, but we also want to ensure that the number of two times are divided into the same sub-array, certainly not two repeated numbers in two different sub-arrays, so that the result is not correct. It is very obvious that the same number of digits on the same bit is the same value, or both are 1. Either is 0, so we can also infer if the bit is one that divides the numbers that appear two times into the same sub-array, which is assumed to be 1, is divided into a sub-array, assuming 0, is divided into a sub-array. This ensures that only one occurrence of the number is present in each sub-array, and the other numbers appear two times, each of which can be obtained by each of these two only occurrences. The time complexity is O (n).

Other than that. All elements are different or later. In finding the rightmost 1, I used a more concise code than the sword, mainly using the following conclusions:

For a number x. The number after x& (-X) is to keep the rightmost 1 in X. All other bits are 0. Note that the-X here is the inverse number of X.-x=~x+1. The ~x here means to reverse the X-bits, not to confuse the two.

The following is the code for AC:

 #include <stdio.h> #include <stdbool.h>/* returns the lowest bit of num 1, everyone else is 0*/int FindFirstBit1 (int num) {//both and after the resulting number, keep Num's rightmost 1. All other bits are placed in order to 0return Num & (-num);} /* Infer whether the specific bit in data is 1. Here the specific bits to infer are determined by the Res. Only one of the res is 1, and the other bits are 0. Returned by the FINDFIRSTBIT1 function, and the bits to be inferred in data are the bits of this unique 1 in res */bool IsBit1 (int data,int res) {return (data&res) ==0)? false:true ;} void findnumsappearonce (int *arr,int len,int *num1,int *num2) {if (Arr==null | | len<2) return;int i;int AllXOR = 0;//All XOR for (i=0;i<len;i++) allxor ^= arr[i];int res = FindFirstBit1 (allxor); *num1 = *num2 = 0;for (i=0;i<len;i++) {if (IsBit1 (Arr[i],res)) *num1 ^= arr[i];else*num2 ^= arr[i];}} int main () {static int arr[1000000];int n;while (scanf ("%d", &n)! = EOF) {int i;for (i=0;i<n;i++) scanf ("%d", arr+i); int num1,num2; Findnumsappearonce (ARR,N,&NUM1,&NUM2); if (Num1 < num2) printf ("%d%d\n", num1,num2); elseprintf ("%d%d\n", NUM2,NUM1);} return 0;} 
/************************************************************** &NBSP;&NBSP;&NBSP;&NBSP; problem:1351 &NBSP;&NBSP;&NBSP;&NBSP; user:mmc_maodun &NBSP;&NBSP;&NBSP;&NBSP; language:c &NBSP;&NBSP;&NBSP;&NBSP; result:accepted &NBSP;&NBSP;&NBSP;&NBSP; time:780 Ms &NBSP;&NBSP;&NBSP;&NBSP; memory:4820 KB ****************************************************************/


The number of only one occurrence in the array of "offer of Swords" (1)

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.