1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* singleNumber(int* nums, int numsSize, int* returnSize) {
int xor = 0;
for(int i = 0;i < numsSize;i++)
xor ^= nums[i];
int temp = xor;
temp &= ~temp+1;
int ansX = 0;
for(int i = 0;i < numsSize;i++)
if(nums[i]&temp)//这一行还有点纳闷,如果换成(nums[i]&temp == temp)就会WA
ansX ^= nums[i];
int ansY = ansX^xor;
*returnSize = 2;
int *ans;
ans = malloc(sizeof(int)*2);
ans[0] = ansX;
ans[1] = ansY;
return ans;
}