260. Single number III

Source: Internet
Author: User

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:

      1. The order of the result is not important. The above example, is [5, 3] also correct.
      2. 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

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.