【Leetcode】 - Single Number II
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的更多相关文章
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number II
int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【Leetcode】【Medium】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【Leetcode】Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
随机推荐
- Java JPushV3服务端
因为JPush的官方文档太乱,所以依据原理自行实现. 主要技术就是post数据到https上和https的auth,实现起来还是很容易的. http://pan.baidu.com/s/1sjEc74 ...
- shell 脚本执行,出现错误bad interpreter: No such file or directory
出现bad interpreter:No such file or directory的原因是文件格式的问题.这个文件是在Windows下编写的.换行的方式与Unix不一样,但是在VI下面如果不Set ...
- 安装ie10慢的解决办法
下面是win7安装ie10的先决条件: http://support.microsoft.com/kb/2818833
- MSSQL数据库逻辑文件名修改与查看
逻辑文件名是什么 你用的程序连接的时候使用的是数据库名,然后你在你的数据库右击属性的时候,左上角单击"文件",可以看到,数据库名和逻辑文件名是不一样的,你可以看自己的数据库的路径下 ...
- WebService 的创建,部署和使用
WebService,即Web服务,能使得运行在不同机器上的不同应用无须借助,专门的第三方软件或硬件,就可相互交换数据或集成. 第一次选择WebService,是为了替代数据库远程连接.我们都知道当S ...
- python简单爬虫编写
1.主要学习这程序的编写思路 a.读取解释网站 b.找到相关页 c.找到图片链接的元素 d.保存图片到文件夹 ..... 将每一个步骤都分解出来,然后用函数去实现,代码易读性高. ##代码尽快运行时会 ...
- ARM你必须知道的事儿——为啥“PC = PC + 8”?
为啥是“PC = PC + 8”: “PC = PC + 8”其实这样写容易让人蒙了.“PC = PC + 8”真正含义应该是: 执行处代码地址 = PC - 8: 也就是说,”PC指向的地址“领先“ ...
- struts2指定集合元素的泛型
public class LoginAction implements Action{ private List users; public void setUsers(List users){ th ...
- ResourceBundle和Properties(转载)
转载: 一般来说,ResourceBundle类通常是用于针对不同的语言来使用的属性文件. 而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的.那么使用Properties类就可以了 ...
- CentOS6.5升级手动安装gcc4.8.2
一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...