LeetCode 260 Single Number III (only the Number III appears once )(*)

Source: Internet
Author: User

LeetCode 260 Single Number III (only the Number III appears once )(*)
Original

Given a numeric array nums, two elements appear only once, and all other elements appear twice. Find the two elements that appear only once. For example, if nums = [1, 2, 1, 3, 2, 5] is specified, [3, 5] is returned. Note: 1. The order of returned results is not important. Therefore, in the preceding example, [3, 5] is returned. 2. Your algorithm should run in linear time complexity. Can you implement it with only the complexity of the constant space?
Original
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two 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. So in the above example, [5, 3] is also correct.2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Analysis

I have seen this question in "offoffoffer". I remember to use bitwise operations to implement it myself. However, I read it again and there is no rule in the book, so I finally pasted the code in the book ......

In fact, there is such a nature:

Any number is different or its own value is equal to zero.

Some readers may not know what an exception or is, so let's intercept it:

ExclusiveOR, abbreviated as xor, or a mathematical symbol used for logical operations. The mathematical symbols are "strong" and the computer symbols are "xor ".

A between B = (? A branch B) Branch (a branch? B)

True success false-> true
False success true-> true
True success true-> false
False success false-> false

That is to say, the two values are the same, the result is false, and the two values are the same or true. So any number is different or its own value is equal to zero, because the two values are the same.

Now, let's get started. Take the array given in the question as an example:

[1, 2, 1, 3, 2, 5, 6, 5, 4, 6] are converted into binary data as follows: 0001, 0010, 0001, 0011, 00100101, 0110, 0101, 0100, 0110, and 0111 all arrays undergo an exclusive or operation. The result is as follows: 0001 (1), 0010 (2), 0001 (1), 0011 (3), 0010 (2) Group B: 0101 (5), 0110 (6), 0101 (6), 0100 (4), 0110 (5), the result of the re-exclusive or operation on group A is: 0011 (3) the result of another XOR operation on group B is: 0100 (4)

The function that determines whether the nth digit of the binary number is 1:

bool IsBit1(int num, unsigned int index) {    num = num >> index;    return (num & 1);}`

Find the first 1 function in the binary number:

unsigned int FindFirstBigIs1(int num) {    int indexBit = 0;    while (((num & 1) == 0) && (indexBit < 8 * sizeof(int))) {        num = num >> 1;        ++indexBit;    }    return indexBit;}

According to the LeetCode function template, modify the following:

vector
  
    singleNumber(vector
   
    & nums) {    vector
    
      singleV;    if (nums.size() <= 0) return singleV;    int resultExclusiveOR = 0;    for (int i = 0; i < nums.size(); ++i)        resultExclusiveOR ^= nums[i];    unsigned int indexOf1 = FindFirstBigIs1(resultExclusiveOR);    int singleNum1 =0, singleNum2 = 0;    for (int j = 0; j < nums.size(); ++j) {        if (IsBit1(nums[j], indexOf1))            singleNum1 ^= nums[j];        else            singleNum2 ^= nums[j];        }    singleV.push_back(singleNum1);    singleV.push_back(singleNum2);    return singleV;}
    
   
  

There are two other related questions:

LeetCode 136 Single Number (a Number that appears only once)
LeetCode 137 Single Number II (the Number II appears only once )(*)

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.