260. Single number III

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.