2014-10-20 11:23:39 +08:00
|
|
|
// Source : https://oj.leetcode.com/problems/single-number/
|
|
|
|
// Author : Hao Chen
|
|
|
|
// Date : 2014-06-17
|
|
|
|
|
2014-10-21 10:49:57 +08:00
|
|
|
/**********************************************************************************
|
|
|
|
*
|
|
|
|
* Given an array of integers, every element appears twice except for one. Find that single one.
|
|
|
|
*
|
|
|
|
* Note:
|
|
|
|
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
|
|
|
|
*
|
|
|
|
*
|
|
|
|
**********************************************************************************/
|
|
|
|
|
2014-10-20 11:23:39 +08:00
|
|
|
#include <stdio.h>
|
2014-11-18 10:45:09 +08:00
|
|
|
// This is classical interview question
|
|
|
|
// As we know, the same number XOR together will be 0,
|
|
|
|
// So, XOR all of numbers, the result is the number which only appears once.
|
2014-10-20 11:23:39 +08:00
|
|
|
int singleNumber(int A[], int n) {
|
|
|
|
int s = 0;
|
|
|
|
for(int i=0; i<n; i++){
|
|
|
|
s = s^A[i];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int a[]={1,1,2,2,3};
|
|
|
|
printf("%d\n", singleNumber(a,5));
|
|
|
|
return 0;
|
|
|
|
}
|