[Leetcode] 260.Single number III

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 t Wice. 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, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could implement it using only constant space complexity?
Test instructions
Given an array where only two elements occur once, the other elements appear two times. Find these two elements.
Ideas:
We know that if there is only one element in an array that appears once, the other element is a two-time problem-solving idea. All the elements need to be different or later, then the result of the XOR is the final individual element. This idea is very simple, that is, each number is understood as the corresponding bits, then those who appear two times, they appear in 1 all positions, 1 appear two times, XOR or end is 0. And for that single occurrence of the element, it each bits corresponding number appears once. So the final XOR result is equivalent to removing all elements that occur two times.
With the basic idea above, we can divide the array into two parts, with only one element appearing once in each section, and the remaining elements appearing two times. You can then use this method to find out the two elements. Suppose that the two elements are X, Y, and x!=y, then the result of all the elements being the same is res = X^y. Obviously, res!= 0, if res=0, then x=y, does not match test instructions. Since Res. =0, then we can find out that one of the binary representations is 1, we're looking from low to high, and the first bits is 1. For the original array, we can divide the array into two parts based on whether the position is 1. X, y is in a different two sub-array. And for other paired occurrences, either the array where x is located, or the array where Y is located. If not, that means that the logarithm is 01 on this one and 1, which clearly does not match the condition that they are a pair of equal numbers.

Above.
The code is as follows:

class Solution {public:vector<int> singlenumber (vector<int>& nums) {
        if (Nums.size () < 2) return vector<int> ();
        else if (nums.size () = = 2) return nums;
        int all = 0;
        for (auto num:nums) all ^= num;
        int flag = 0x01;
            while (true) {if (Flags & All) is break;
        Flag <<= 1;
        } int res1 = 0, res2 = 0;
            for (auto num:nums) {if (num & flag) res1 ^= num;
        else res2 ^= num;
        } vector<int> result;
        Result.push_back (RES1);
        Result.push_back (Res2);
    return result; }
};

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.