【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 ...
随机推荐
- SkinSoft中.vssf样式文件在VS2005中的应用(图文)
前些天在项目中应用了皮肤控件,涉及到了(.ssk),也做了相应的记录. 但还是觉得.ssk的并不算满意,所以今天又粗略的研究了一下SkinSoft这个软件: 如题,他的作用就是自定义控件与窗体样式. ...
- 【网络收集】order by 自定义排序
使用order by排序,有时候不是根据字符或数字顺序,而是根据实际要求排序. 例如有客户A,B,C,我希望排序结果是B,C,A,那么就要通过自定义的规则排序. 第一种方法,可以构造一张映射表,将客户 ...
- DynamicObject数据包操作
DynamicObject的结构非常简单明了,就是一个字典,类似于一个Dictionary<string, object>,其中的object可能是一个简单值(普通字段),可能是一个复杂值 ...
- asp.net全局记住值
每个网页都要记住名字和密码! 在Page_Load中写如下代码: Session[“起个名字”]=要赋值的; 在显示的时候写 Response.write(Session[“当时起的名字”.ToStr ...
- C#中如何查找Dictionary中的重复值
简介 在这篇帮助文档中,我将向你展示如何实现c#里字典中重复值的查找.你知道的对于一个老鸟来说,这是非常简单的代码.但是尽管如此,这也是一篇对c#初学者非常有用的帮助文档. 背景 多数程序员对小型数据 ...
- java如何准确的读取多音字
java如何准确的读取多音字 java准确读取多音字的方法,多音字的识别一直是一个问题,笔者结合了很多不同的读取方法,完成了这个扩展的帮助类. 首先,下载java读取拼音的jar(pinyin4j-2 ...
- C语言知识总结(3)
数组 数组的特点: 只能存放一种类型的数据,比如int类型的数组.float类型的数组 里面存放的数据称为“元素” 初始化方式 ] = {, , }; ] = {,}; , , }; ] = {[]= ...
- 深入探析koa之异步回调处理篇
在上一篇中我们梳理了koa当中中间件的洋葱模型执行原理,并实现了一个可以让洋葱模型自动跑起来的流程管理函数.这一篇,我们再来研究一下koa当中异步回调同步化写法的原理,同样的,我们也会实现一个管理函数 ...
- C++ 四则运算简单设计
如果说要用C++写一个简单的四则运算的程序,相信难不到人,这还不简单吗?然后用不了五分钟,三下五除二,就出了下面的代码,一调试,没问题..... #include <iostream> u ...
- 比较不错的一个ios找茬游戏源码
找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...