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?
1 /**2 * Return An array of size *returnsize.3 * Note:the returned array must is malloced, assume caller calls free ().4 */5 int* Singlenumber (int* Nums,intNumssize,int*returnsize) {6 intFlag =0;7 inti;8 intK =1;9 int*Res;Tenres = (int*)malloc(2*sizeof(int));//It must be malloc or not. Oneres[0] = res[1] =0; A for(i =0; i < numssize; i++)//first different or out of these 2 numbers -Flag ^=Nums[i]; - while(1) the { - if(k & Flag)//find the first bit of 1 in binary, then these 2 digits, one of which is 1, the other is 0 - Break; -K = k <<1; + } - for(i =0; i < numssize; i++) + { A if(K & Nums[i])//put all of this in a 1-bit category, atres[0] ^= Nums[i];//different or out this is 0 of the number - Else //In the same place as the 1 number -res[1] ^=Nums[i]; - - } -*returnsize =2; in returnRes; -}
260. Single number III