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. Javascript之日历

    <meta http-equiv="content-type" content="text/html; charset=utf-8"> <ti ...

  2. dateset是不是在缓存中

    C#开发erp系统的时候有一个多表数据的查询展示到页面,采用了存储过程的方式,但是存储过程中没有加入分页(菜比).刚开始测试数据几百条没有问题,当数据量提升至十万级后页面加载速度就很卡了,一般是使用分 ...

  3. HTTPS协议加密解密全过程(图解)

    我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL ...

  4. ant条件逻辑

    <condition property="sdk-folder" value="E:\android\android-sdk\adt-bundle-windows- ...

  5. 实验三——SDRAM

    一.运行环境 开发板:jz2440 系统:  ubuntu12.04 编译器:arm-linux-gcc 二.特殊寄存器 sdram的操作无需按照时序图来设置,只要设置好相关的13个寄存器,arm处理 ...

  6. OpenSSL 安全漏洞: heartbleed

    Heartbleed 是 2014年4月7日被广泛报道的一个 OpenSSL 安全漏洞,号称是灾难. 利用它能读取服务器上最多64k的内存,只要该服务器可以通过ssl连接.   Heartbleed ...

  7. 看部电影,透透彻彻理解IoC(你没有理由再迷惑!)

    引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础上开花结果.但是IoC这个重要的概念却比较晦涩隐讳,不容易让人望文生义,这不能不 ...

  8. UINavigationController 与 UITabBarController

    http://www.cnblogs.com/YouXianMing/p/3756904.html // index start from 1. UITabBarItem *newsItem = [[ ...

  9. php生成圆形图片

    http://files.cnblogs.com/files/adtuu/circle_image.zip

  10. php中each()与list()函数

    <?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');reset($fruit); ...