"One Day together Leetcode" #260. Single number III

Source: Internet
Author: User

One Day together Leetcode

This series of articles has all been uploaded to my github address: Zeecoder ' s GitHub
You are welcome to follow my Sina Weibo, my Sina Weibo blog
Welcome reprint, Reprint please indicate the source

(i) Title

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, 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?

(ii) Problem solving

The main idea: there are two numbers in an array only once, the other number appears two times, find out the two numbers

Problem-solving ideas: A comparison of "one Day together Leetcode" #137. Single number, if there are two occurrences only once, then use the XOR operation to find out the XOR value of the two numbers

From this XOR value, these two numbers are different, then you can find the different bits, take a different bit, such as the first M bit.

The whole array is divided into two categories, one for the M-bit is 1, the class m is 0, so that in each group number only a single occurrence of the number, it is easy to use the XOR operation to solve.
See the code for specific ideas:

classSolution { Public: vector<int>Singlenumber ( vector<int>& Nums) { vector<int>RetintSize = Nums.size ();if(Size = =0)returnRetinttemp = nums[0]; for(inti =1; i < size; i++) temp^=nums[i];//Find the final value of all or after        unsigned intBit for(inti =0; I < +; i++) {//Find the first 1 bit in tempbit=1<<i;if((temp&bit) ==bit) Break; }intTemp1 =0, Temp2 =0;//TEMP1 is the first number that appears only once, Temp2 is the second         for(inti =0; i < size; i++) {if((nums[i]&bit) = =0) {//Group, 0 on the first m bitTemp1^=nums[i]; }Else{Temp2^=nums[i];//Group 1 on the M-bit}} ret.push_back (TEMP1); Ret.push_back (TEMP2);returnRet }};

Related Topics:
"One Day together Leetcode" #137. Single number II
"One Day together Leetcode" #137. Single number

"One Day together Leetcode" #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.