# leetcode Single Number II

### Single Number II

Total Accepted: 4273 Total Submissions: 14218

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

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

Similar to I, when the number accumulates for three times, set the bit to 0. I.e., how to simulate a ternary system using a binary system.

I wrote an wrong code like :

class Solution {
public:
int singleNumber(int A[], int n) {
int ones = 0, twos = 0, threes = 0, i, res = 0;
if (n == 1)
return A[0];
for (i = 0; i < n; ++i) {
res |= A[i];
threes = twos & A[i];
res ^= threes;

twos = (twos | (ones & A[i])) ^ threes;
ones = ones ^ A[i];
}
return res;
}
};

The mistake is that the variable ones needs to be cleared based on the variable threes. At the same time, the variable res seems redundant. A right code is :

class Solution {
public:
int singleNumber(int A[], int n) {
int ones = 0, twos = 0, threes = 0, i, res = 0;
if (n == 1)
return A[0];
for (i = 0; i < n; ++i) {
//res |= A[i];
threes = twos & A[i];
//res ^= threes;

twos = (twos | (ones & A[i])) & (~threes);
ones = (ones ^ A[i]) & (~threes);
}
return ones;
}
};
However, the code seems not in a standard way. I tried to fix the code in an accessible code:

class Solution {
public:
int singleNumber(int A[], int n) {
int ones = 0, twos = 0, threes = 0, mask = -1, i, res = 0;
if (n == 1)
return A[0];
for (i = 0; i < n; ++i) {
//res |= A[i];
threes = twos & A[i];
//res ^= threes;
twos = (twos | (ones & A[i])) & mask;
ones = (ones | A[i] ) & mask;
}
return ones;
}
};

At last, the problem could be changed like this:

Given an array of integers, every element appears m times except for one for n times. Find that the one of n times.

  int findNumber(int A[], int size, int m, int n) {
int mask = -1, i, j;
vector<int> times(m+1,0);

for (i = 0; i < size; ++i) {
times[m] = 0;
for (j = m; j >= 1; --j) {
times[j] = (times[j] | (times[j-1] & A[i])) & mask;
}
}
return times[n];
}

• 本文已收录于以下专栏：

## LeetCode 137：Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. ...
• sunao2002002
• 2015年06月01日 22:36
• 5026

## 【LeetCode-面试算法经典-Java实现】【137-Single Number II（只字出一次的数字II）】

【137-Single Number II（只出现一次的数字II）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　Given an array of integers, ...
• DERRANTCM
• 2015年08月18日 06:24
• 4191

## LeetCode || Single Number II

Single Number II  Total Accepted: 12144 Total Submissions: 37399My Submissions Given an ...
• 2014年04月09日 23:35
• 18977

## LeetCode(137)Single NumberII

• feliciafay
• 2014年02月09日 10:15
• 3035

## LeetCode题解–137. Single Number II

• sinat_14867719
• 2017年04月13日 15:32
• 178

## LeetCode--single-number-ii

• wl_ss
• 2018年01月20日 21:01
• 51

## LeetCode：Single Number I & II & III

（一）题目描述：single-number Given an array of integers, every element appears **twice** except for one. Fi...
• u011489043
• 2017年03月21日 14:26
• 267

## leetcode single number II

• donggua_fu
• 2017年03月02日 09:02
• 134

## LeetCode137. Single Number II

137. Single Number II 1、原题 Given an array of integers, every element appears three times except for...
• u010217031
• 2017年04月16日 02:24
• 73

## leetcode_middle_41_137. Single Number II

• pusude
• 2017年02月13日 21:39
• 60

举报原因： 您举报文章：leetcode Single Number II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)