137.Single number II (Method 1 sequencing 2STL container map hashing 3-bit operation 4 improved bit operation)

Source: Internet
Author: User

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

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

Hidetags

Bit Manipulation



#pragma once#include<iostream> #include <map>using namespace std;//Method 1: Sorting//Method 2:stl container Mapint singleNumber2 ( int a[], int n) {map<int, int> m;map<int, int>::iterator it;for (int i = 0; i < n; i++) {it = M.find (a[i]); I F (It = = M.end ()) m[a[i]] = 1;else//m[a[i]]++;it->second++;} for (int i = 0; i < n; i++) if (m.at (a[i]) = = 1)//at vs. find: At returns the value corresponding to the index, possibly out of bounds return a[i];} Method 3: Bitwise operation.  Consider all binary representations, if we take the sum of all the numbers in the ith position and 3, then only//will have two results 0 or 1 (according to test instructions, 3 0 or 3 1 add remainder is 0). So the result of the remainder is that "single number". int singleNumber3 (int a[], int n) {//int Four bytes 32 bits, per bit and by one digit record int count[32] = {0};//Each element is placed 0int ResU lt = 0;for (int i = 0; i <; i++)//32-bit {for (int j = 0; J < N; j + +)//n number if (A[j] >> 1 & 1)//Note that the operation has only one &amp ;, judge only two Count[i]++;result |= ((Count[i]% 3) << i);//good experience. | : bit or}return result;} Method 4: Improved bit operation. int singleNumber4 (int a[], int n) {int ones = 0, twos = 0, threes = 0;for (int i = 0; i < n; i++) {//note, before each cycle, ones twos t Hrees Disjoint twos |= ones&a[i];//new 2 = original 2 not A[i] + original 2 in A[i]ones ^= a[i];//New 1 = original 1 not a[i] + original non 1 in a[i]//thereupon, new 2 & New 1 = original 2 in A[i]. That is, the intersection part appears 3 times, the intersection part in ones and twos 0,ones twos on the guarantee a[i] after reasonable threes = ones&twos;//Note threes meaning is not a[i] three times the bit but, The newly generated three-bit ones ^= threes;//ones &= ~threes in this layer, but the former is faster twos ^= threes;//twos &= ~threes; Yes, but the former is faster}return ones;} void Main () {int a[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5};cout << singleNumber3 (A,) << endl;system ("p Ause ");}


137.Single number II (Method 1 sequencing 2STL container map hashing 3-bit operation 4 improved bit operation)

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.