【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 ...
随机推荐
- .Net简单上传与下载
上传: 首先上传我们需要一个控件-FileUpLoad: 再加上一个上传按钮: 在上传按钮的Click事件中添加如下代码: FileUpload1.SaveAs(Server.MapPath(&quo ...
- 六.CSS浮动与清除
浮动 把元素从常规文档流中取出.是元素脱离常规文档流 浮动的作用: ①实现文本绕排图片效果 ②让原本在上下方向上堆叠的块级元素可以变成左右并列,从而实现多栏布局 文本绕排图片 首先HTML代码如下 & ...
- 十个顶级的C语言资源助你成为优秀的程序员
译者言:学习C语言,需要一点一滴,沉下心来,找个安静的地方,泡上一杯咖啡,在浓郁的香味中一起品味她.(by Boatman Yang) 人们通常认为计算机编程很烦,但是有些人却从中发现了乐趣.每一个程 ...
- 四步完成ajax的使用
什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页. 如何使用ajax? 第一步,创建xmlhttprequest对象,var xmlhttp ...
- PHP中大括号{}用法总结
刚用到一个由字符串来设定对像属性名的功能.发现大括号的作用真强…. 1. 动态设置对象的属性名的使用:写法一(不能正确设置): $obj->$string[$key]; //这里只能使用$str ...
- redistribute_prefix
使用分发列表和前缀列表控制路由 拓扑如下 将基本环境(ip和路由协议)配置好,所得到的各个路由表如下 R1: R2: R3: EIGRP和OSPF间的双向重分发 1. 在R2上做重分发 2. ...
- C# 获取文件名及扩展名
C# 获取文件名及扩展名 string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastI ...
- 用LINQ在集合中查询特定对象
这里是原文出处: 简单的概括LINQ LINQ是Language-Integrated Query的缩写,是C# 3.0和VB 9.0中新加入的语言特性,可以在编程时使用内置的查询语言进行基于集合的操 ...
- iOS数据持久化(三)
#pragma mark - Core Data stack /** * @synthesize 关联成员变量和属性 */ @synthesize managedObjectContext = _ma ...
- 2015-0306—DataLList
DataList具有repeater的所有功能,不同的是DataList自动将模板绘制成为一个表格,每一行数据都绘制成<tr>. 一.SQL的准备工作: 按照以下代码创建: create ...