Leetcode--single number II (finds only one occurrence in the array 2)

Source: Internet
Author: User

Questions:

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?

Single number I upgrade, the other number in an array appears 3 times, one occurrence only once, find that number.

Analysis:

The other number appeared 2 times in question I, a number only appeared 1 times, and the number was found, and the problem was solved easily by using an XOR operation.

In question II the title became, the other number appeared 3 times, a number only appeared once, to find out the number, and then simply use the XOR operation obviously can not deny this problem, because the other number also appeared in a single number of times.

Here, a solution to solve the problem, but not very good, the great God with a better way, here in the back will be posted, but because it is not understood, here is not to do the discussion, then the next simple talk about this is not a good way.

Because the other number has occurred 3 times, a number has only one occurrence, from the bit to consider that is, each occurrence of the number of 3 times on each bit has appeared 3 times, here 13 for example:

1 2 3 4 5 6 7 8
0 0 0 0 1 1 0 1

Then 3 13 words, each one is summed, then must be a multiple of 3,

For example:

Bit 1, added after and 0 times times for 0,3;

Bit 5,6,8, and both are 1 time times the size of the 3,3.

Therefore, we can separately calculate each of the array of the number of and, and then to 3 to find the remainder is only 1 times the value of the current bit (0 or 1).

Code:

 Public classSolution { Public intSinglenumber (int[] a) {intresult = 0;  for(inti=31;i>=0;i--){            inttemp = 0; Result= Result<<1;  for(intj=0;j<a.length;j++) {Temp+ = (a[j]>>i) & 1; } result|= temp%3; }        returnresult; }}

The Code of the Great God:

 Public class Solution {    publicint singlenumber (int[] A) {        int ones = 0, twos = 0;          for (int i = 0; i < a.length; i++) {        = (ones ^ a[i]) & ~twos;         = (twos ^ a[i]) & ~ones;        }         return ones;}    }

Leetcode--single number II (finds only one occurrence in the array 2)

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.