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. Linux常用命令英文全称与中文解释Linux系统

    Linux常用命令英文全称与中文解释Linux系统(转)   Linux常用命令英文全称与中文解释Linux系统 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Pri ...

  2. Part 10 Stored procedures in sql server

    Stored procedures in sql server Stored procedures with output parameters Stored procedure output par ...

  3. JOSN传字符串方法

    #region 提示信息 /// <summary> /// 操作失败(无参数) /// </summary> /// <returns></returns& ...

  4. 【网络收集】如何修改vs tfs的登录名和密码 .

    连接TFS时,如果本机保存了用户的网络密码,不会出现用户名和密码的输入框,若要更换TFS的用户名和密码,需按以下步骤操作:控制面板--->用户账号--->管理网络密码,此时会列出所有保存了 ...

  5. 你所不知道的html5与html中的那些事第三篇

    文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后,所有的工作 ...

  6. python简单爬虫编写

    1.主要学习这程序的编写思路 a.读取解释网站 b.找到相关页 c.找到图片链接的元素 d.保存图片到文件夹 ..... 将每一个步骤都分解出来,然后用函数去实现,代码易读性高. ##代码尽快运行时会 ...

  7. Facade模式和Mediator模式

    相同的目的:把某种策略施加到另一组对象上. Facade从上面施加策略. 其使用是明显且受限的.当策略涉及范围广泛并且可见时. 约定的关注点.都同意使用Facade而不是隐藏于其下的对象. Media ...

  8. [Bootstrap]组件(二)

    按钮组 .btn-group>.btn : 一组.btn按钮包裹在.btn-group 外包元素.btn-group  {position/display/} 按钮元素.btn <div ...

  9. C++ Maps 映射

    C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空 ...

  10. Apache HTTP Server安装教程

    Apache HTTP Server安装教程 Apache HTTP Server的官方网站是:http://httpd.apache.org/,可以从中下载最新版本的Apache HTTP Serv ...