Leetcode OJ Single number III (unique digit-three)

Source: Internet
Author: User

Topic:

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?
Ideas:

Related Topics:

Leetcode OJ Single number (unique digit)

Leetcode OJ Single number II (unique digit-two)

Reference: http://blog.csdn.net/u012243115/article/details/45286963.

Code:

Class Solution {public:vector<int> Singlenumber (vector<int>& nums) {vector<int> ResU        Lt        int len = Nums.size ();                    if (Len < 2) return result;        int tmp = 0;                    for (int i = 0; i < len; i++) tmp ^= nums[i];        int indexofone = 0; for (int i = 0; i <; i++) {if (TMP & (1 << i)! = 0)//NOTE: bit operator & has a low priority, plus ().                It is better to use 1 for the shift operation than for the TMP, because if the TMP is negative, the right side will be 1. (although this does not affect the result, it is best not to do a shift operation on tmp) {indexofone = i;            Break        }} int tmp2 = (1 << indexofone);        int NUM1 = 0, num2 = 0;            for (int i = 0; i < len; i++) {if (Nums[i] & tmp2) num1 ^= nums[i];        else num2 ^= Nums[i];        } result.push_back (NUM1);        Result.push_back (NUM2);    return result; }};


Copyright NOTICE: Reprint please indicate the source.

Leetcode OJ Single number III (unique digit-three)

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.