137. Single number II (Bit)

Source: Internet
Author: User

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?

Ideas:

STEP1: From single number we know that through easy or operation can be stored in an int for a few times

STEP2: We can get the number of double-digit occurrences by doing with the singular.

STEP3: Try to get rid of the STEP1 int three times.

classSolution { Public:    intSinglenumber (intA[],intN) {intonce =0; inttwice =0;  for(inti =0; I < n; i++) {twice|= once & A[i];//The num appeared 2 timesOnce ^= a[i];//The num appeared 1 times            intNot_three = ~ (once & twice);//The num not appeared 3 timesOnce = Not_three & once;//Remove num appeared 3 times from oncetwice = Not_three & twice;//Remove num appeared 3 times from twice        }          returnonce; }};

137. Single number II (Bit)

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.