Single Number,single number II

Source: Internet
Author: User

Single numberTotal accepted:103745 Total submissions:218647 difficulty:medium

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

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

(m) Single number II (m) Single number III (m) Missing number (H) Find the Duplicate number
 class   solution { public  :  int  Singlenumber ( Vector<int  >& Nums) { int  res = 0  ;         int  nums_size = Nums.size ();  for  (int  i= 0 ; I<nums_size;i++) {res  ^= Nums[i];     return   res; }};
Next challenges: (m) Single number II (m) Single number III single number IITotal accepted:69333 Total submissions:191725 difficulty:medium

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

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

Count the number of 1 in each bits, this method is suitable for this type of topic: All the numbers in the array appear K, only one appears once

Const intBITS =sizeof(int) *8;classSolution { Public:    intSinglenumber (vector<int>&nums) {        inttimes[bits]={0}; cout<<BITS<<Endl; intNums_size =nums.size ();  for(intI=0; i<nums_size;i++){            intx =Nums[i];  for(intj=0; j<bits;j++){                if((X&GT;&GT;J) &1) {Times[j]++; }            }        }        intres =0;  for(intI=0; i<bits;i++){            if(times[i]%3) {res+=1<<i; }        }        returnRes; }};

Single number IIITotal accepted:18186 Total submissions:44516 difficulty:medium

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

All results differ or are left with a non-0 value, which is the difference or result of only one occurrence of two digits, the number of the first bit=1 in this number, and 1 left to move so many digits into the array of the splitter.

Const intBITS =sizeof(int)*8;classSolution { Public: Vector<int> Singlenumber (vector<int>&nums) {        intNums_size =nums.size (); intn =0;  for(intI=0; i<nums_size;i++) {n^=Nums[i]; }        intSeprator =0;  for(intI=0; i<bits;i++){            if((n>>i) &1) {Seprator=1<<i;  Break; }} vector<int>Res; intres1=0, res2=0;  for(intI=0; i<nums_size;i++){            if((Seprator &Nums[i])) {Res1^=Nums[i]; }Else{res2^=Nums[i];        }} res.push_back (RES1);        Res.push_back (Res2); returnRes; }};
Next challenges: (E) Number of 1 Bits (m) Bitwise and of Numbers Range (m) Maximum Product of Word Lengths

Single Number,single number II

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.