260. Single number IIITotal Accepted:
30927 submissions:
71149 Difficulty:
Medium
Given An array of numbers nums
, in which exactly-elements appear only once and all the other elements appear exactly Twice. Find the elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
Note:
- The order of the result is not important. The above example, is
[5, 3]
also correct.
- Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
Code:
/**
* Return An array of size *returnsize.
* Note:the returned array must is malloced, assume caller calls free ().
*/
int* Singlenumber (int* nums, int numssize, int* returnsize) {
int num = 0;
int i;
int *tmp = (int*) malloc (2*sizeof (int));
for (i = 0;i<numssize;i++)
Num ^= nums[i];
int lowbit = 1;
while (!) ( Lowbit&num))
{
lowbit<<=1;
}
Tmp[0] = 0;
TMP[1] = 0;
for (i = 0;i<numssize;i++)
{
if ((nums[i]&lowbit)! = 0)
{
Tmp[0] ^= nums[i];
}
Else
{
TMP[1] ^= nums[i];
}
}
*returnsize = 2;
return TMP;
}
260. Single number III