Problem Discription:

Suppose the array A has n items in which all of the numbers apear 3 times except one. Find the single number.

 int singleNumber2(int A[], int n) {
int ret = ;
while(n--)
{
ret ^= A[n];
}
return ret;
}

Related Problem:

Suppose the array A has n items in which all of the numbers apear twice except one. Find the single number.

Solution 1:

Suppose the required return value is ret. Each bit of ret is calculated by the respective bit of A[0:n-1].

    int singleNumber3(int A[], int n) {
int m=;
int ret = ;
while(m--)
{
ret = ret >> ;
ret &= 0x7fffffff;
int sum = ;
for(int i=;i<n;i++)
{
sum += A[i] & 0x00000001;
A[i] = A[i] >> ;
}
if(sum%){
ret |= 0x80000000;
}
}
return ret;
}

Solution2: Solution1 needs 32*n passes of loop. Solution2 is found on the Internet, see http://blog.csdn.net/bigapplestar/article/details/12275381 . However the bit operation is confused. Therefore I write solution3, and try to explain it.

    public int singleNumber(int[] A) {
int once = 0;
int twice = 0;
for (int i = 0; i < A.length; i++) {
twice |= once & A[i];
once ^= A[i];
int not_three = ~(once & twice);
once = not_three & once;
twice = not_three & twice;
}
return once;
}
Solution3:

My solution3 seems more easy-to-understand compared with solution2. Suppose the i-th bit of one, two, thr (onei, twoi, thri) is used to count how many bits in A[0-n-1]i is 1.

# onei twoi thr_i

1 1       0      0

2 0       1      0

3 0       0      0

4 1       0      0

5 0       1      0

6 0       0      0 ....

so we have:

if(A[i] == 1)

  if(one == 0) one = 1;

  else one = 0;

    if(two == 0) two = 1;

    else two = 0;

      if(thr == 0) thr = 1;

      else thr = 0;

  when thr=1, one=two=0;

So with the bit operation we have an easy-to-understand version as below:

int singleNumber3(int A[], int n) {
int one=, two=, thr=;
while(n--)
{
//thr ^= (one & two & A[n] );
two ^= one & A[n];
one ^= A[n];
thr = one & two;
one = (~thr) & one;
two = (~thr) & two;
//thr = (~thr) & thr;
}
   return one;
}

Hope this may help.

【Leetcode】 - Single Number II的更多相关文章

  1. 【题解】【位操作】【Leetcode】Single Number II

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

  2. 【leetcode】Single Number II (medium) ★ 自己没做出来....

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

  3. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  6. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

  7. 【Leetcode】【Medium】Single Number II

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

  8. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  9. 【Leetcode】Single Number

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

随机推荐

  1. HDU4277 USACO ORZ(dfs+set)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  2. 转:C#写的WEB服务器

    转:http://www.cnblogs.com/x369/articles/79245.html 这只是一个简单的用C#写的WEB服务器,只实现了get方式的对html文件的请求,有兴趣的朋友可以在 ...

  3. FFT结果的物理意义

    图像的频率是表征图像中灰度变化剧烈程度的指标,是灰度在平面空间上的梯度.如:大面积的沙漠在图像中是一片灰度变化缓慢的区域,对应的频率值很低:而对 于地表属性变换剧烈的边缘区域在图像中是一片灰度变化剧烈 ...

  4. 用LINQ在集合中查询特定对象

    这里是原文出处: 简单的概括LINQ LINQ是Language-Integrated Query的缩写,是C# 3.0和VB 9.0中新加入的语言特性,可以在编程时使用内置的查询语言进行基于集合的操 ...

  5. 检测URL地址是否有响应

    今天突然出来了一个问题,URL地址调用导致程序卡死(原因是服务挂了,磁盘坏了) 然后想到了,再调用URL地址前先判断下地址是否有响应,这样不就可以解决问题了吗? C# 代码: /// <summ ...

  6. Spring MVC与easyui国际化

    1.建立资源文件 在webapp下建立文件夹language,在其中再添加file,命名分别为language.properties,language_en.properties,language_z ...

  7. 借Windows说明Linux分区和挂载点[转]

    在介绍Linux分区和挂载点前,我想先说一个Windows的例子,Windows大家都比较熟,再借这个例子来说明什么是Linux分区和挂载点. 1.消失了的分区 在WinPE下,我将一块硬盘分成一个主 ...

  8. PHP运行方式对比

    文章内容来自以下站点http://www.cnblogs.com/xia520pi/p/3914964.html 关于PHP目前比较常见的五大运行模式: 1.CGI(通用网关接口 / Common G ...

  9. NOSQL之【redis的安全策略】

    原文:http://redis.io/topics/security 1.Redis的安全模式 可信环境下的可信用户才可访问redis.这意味着,将redis服务器直接暴露在Internet或者不可信 ...

  10. 如何查看 Apache 的版本

    查看 Apache 服务器版本的命令行为: httpd -v 或者 apachectl -v 例如:用 Xshell 连接到服务器后,输入:httpd -v 或者:apachectl -v 返回: S ...