The sword refers to the number that appears only once in the array of the offer series source code, and the sword refers to the offer

Source: Internet
Author: User

The sword refers to the number that appears only once in the array of the offer series source code, and the sword refers to the offer

Question 1351: The number time limit that appears only once in the array: 1 second memory limit: 32 MB special question: No submitted: 2582 solution: 758 question description: in an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once. Input: each test case contains two rows: the first row contains an integer n, indicating the array size. 2 <= n <= 10 ^ 6. The second row contains n integers, indicating the array elements, all of which are int. Output: For each test case, only two numbers appear in the output array. The order of output numbers from small to large. Sample input: 82 4 3 6 3 2 5 5 sample output: 4 6


#include <iostream>#include<stdio.h>using namespace std;unsigned int findFirstBitIs1(int num){    int indexBit = 0;    while(((num&0x01)==0)&&(indexBit<8*sizeof(int))){        num=num>>1;        indexBit++;    }    return indexBit;}bool isBit1(int num, int indexBit){    num = num>>indexBit;    return (num&0x01);}void findNumsAppearOnce(int data[],int length,int* num1,int* num2){    if(data==NULL||length<2)        return;      int resultExclusiveOR = 0;      for(int i=0;i<length;i++){        resultExclusiveOR^=data[i];      }   unsigned int pos = findFirstBitIs1(resultExclusiveOR);    *num1=*num2=0;    for(int j=0;j<length;j++){        if(isBit1(data[j],pos)){            *num1^=data[j];        }else{            *num2^=data[j];        }    }}int main(){    int n;    while(scanf("%d",&n)!=EOF){        int* data = new int[n];        int num1=0,num2=0;        for(int i=0;i<n;i++){            scanf("%d",&data[i]);        }        findNumsAppearOnce(data,n,&num1,&num2);        int min,max;        min = num1<num2?num1:num2;        max = num1>num2?num1:num2;        printf("%d %d\n",min,max);    }    return 0;}


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.