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;}