LeetCode136 single number, LeetCode137 single number II, LeetCode260 single number III

Source: Internet
Author: User

136. Single number

Given an array of integers, every element appears twice except for one. Find the single one. (easy)

Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?

Analysis:

The first question belongs to the skill question, has done will, has not done the difficult thought. Consider the XOR operation, the same number of different or after 0, 0 with a number XOR or the number itself, and the XOR operation satisfies the commutative law and the binding law.

So this problem will be all the number of different or up, you can get the number of the order.

Code:

1 classSolution {2  Public:3     intSinglenumber (vector<int>&nums) {4         intresult =0;5          for(inti =0; I < nums.size (); ++i) {6Result ^=Nums[i];7         }8         returnresult;9     }Ten};

137. Single Number II

Given an array of integers, every element appears three times except for one. Find the single one. (Medium)

Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?

Analysis:

In addition to the "single dog" Other elements have appeared three times, consider the idea of the different or in the previous question (do not carry addition, two 1 then become 0), the promotion to three numbers, that is, there is a 1 if three times, should be offset to 0;

So consider creating an array of 32 elements that represent the various positions of int, and then add each bit of each number, and the results of the MoD 3 revert to a number.

Code:

1 classSolution {2  Public:3     intSinglenumber (vector<int>&nums) {4         intbitarray[ +];5memset (BitArray,0,sizeof(BitArray));6         intresult =0;7          for(inti =0; I < +; ++i) {8              for(intj =0; J < Nums.size (); ++j) {9Bitarray[i] + = (nums[j) >> I &1);Ten             } OneBitarray[i]%=3; AResult |= (Bitarray[i] <<i); -         } -         returnresult; the     } -};

260. Single number III

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. (Medium)

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?

Analysis:

There are two single dogs, others are two times. Consider how to turn this problem into a single number1 problem.

Using the idea in 1, first put all the numbers different or, finally you can get a number. It can be known that this number (Xorresult) is the result of the two single dogs, but we cannot recover two numbers from this number itself.

But we can xorresult the first 1 to appear in the position, this position is 1 notes before two numbers in this bit different (one 0, one 1);

So we can put the element in the original array this bit is 0 or 1 into two arrays, these two arrays are single number1 problem, and finally add two results to the vector return.

Code:

1 classSolution {2  Public:3vector<int> Singlenumber (vector<int>&nums) {4         intXorresult =0;5          for(inti =0; I < nums.size (); ++i) {6Xorresult ^=Nums[i];7         }8         intLastbitofone = Xorresult-(Xorresult & (Xorresult-1) );9         intRESULT1 =0, RESULT2 =0;Ten          for(inti =0; I < nums.size (); ++i) { One             if((Nums[i] & lastbitofone) = =0 ) { ARESULT1 ^=Nums[i]; -             } -             Else { theRESULT2 ^=Nums[i]; -             } -         } -vector<int>result{result1, result2}; +         returnresult; -     } +};

LeetCode136 single number, LeetCode137 single number II, LeetCode260 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.