【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路:
这个题是Single Number的进阶版,如果其他的数都出现两次而不是三次,可以将所有的数进行异或,出现偶数次的bit最后会是0,而target数字中为1的bit没有被抵消而保留了下来:
Given an array of integers, every element appears twice except for one. Find that single one.
int singleNumber(int A[], int n) {
int res = ;
for(int i = ; i < n; i++){
res ^= A[i];
}
return res;
}
Single Number的本质,就是用一个数记录每个bit出现的次数,如果一个bit出现两次就归0,这种运算采用二进制底下的位操作^是很自然的。Single Number II中,如果能定义三进制底下的某种位操作,也可以达到相同的效果,但是这个东西没有现成的可用。
我们换个思路,Single Number II中想要记录每个bit出现的次数,一个数搞不定就加两个数,用ones来记录只出现过一次的bits,用twos来记录只出现过两次的bits,ones&twos实际上就记录了出现过三次的bits,这时候我们来模拟进行出现3次就抵消为0的操作,抹去ones和twos中都为1的bits。
int singleNumber(int A[], int n) {
int ones = ;//记录只出现过1次的bits
int twos = ;//记录只出现过2次的bits
int threes;
for(int i = ; i < n; i++){
int t = A[i];
twos |= ones&t;//要在更新ones前面更新twos
ones ^= t;
threes = ones&twos;//ones和twos中都为1即出现了3次
ones &= ~threes;//抹去出现了3次的bits
twos &= ~threes;
}
return ones;
}
【题解】【位操作】【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 @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- (Array,位操作)137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [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 (面试题推荐)
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
- LeetCode | Single Number II【转】
题目:Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
随机推荐
- 上传项目的更改 info.plist文件
info.plistOpen AsSource As 添加<key>NSAppTransportSecurity</key> <dict> &l ...
- android程序打包成APK
1.下载ant(从官网上下载没有bin目录,可以直接在百度上搜APACHE-ANT-1.9.4-BIN.ZIP) 2.解压到C盘根目录(也可以是其他盘) 3.修改环境变量 ANT_HOME C: ...
- pthread_create如何传递两个参数以上的参数
涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程 定义一个结构体 struct mypara { var para1;//参数1 var para2;//参数2 } 将这个结 ...
- GET /hello/fred/0926xxx572
GET /hello/fred/0926xxx572 app.get('/hello/:name/:tel', function(req, res) { console.log(req.params. ...
- BPM与OA,选择好烦恼!
企业在信息化建设的过程中 难免遭遇BPM和OA之争, 难!难!难! 选择BPM吧, 有人觉得你“打肿脸充胖子”: 选择OA嘛, “行业大哥”们已经在BPM的路上越走越远… 其实, OA和BPM从某种意 ...
- Processon 一款基于HTML5的在线作图工具
CSDN的蒋涛不久前在微博上评价说ProcessOn是web版的visio,出于好奇私下对ProcessOn进行了一番研究.最后发现无论是在用户体验上,还是在技术上,ProcessOn都比微软的Vis ...
- matlab中如何保留指定的变量的数据
在使用matlab时,通常要将计算的结果保存到mat文件中.使用的命令是: save filename 但是用上述命令保存的时候,会将计算过程中的其他变量一起保存了.这中粗糙的保存有以下缺点: (1) ...
- [C/C++]数据类型与变量
1.类型: 1.1.简介 1.1.1.基本类型(base type)和复合类型(compound type): 复合类型是指基于其它类型定义的类型.([1].2.3 p45)那么基本类型就是复合类型基 ...
- Core Text概述
本文是我翻译的苹果官方文档<Core Text Overview> Core Text框架是高级的底层文字布局和处理字体的技术.它在Mac OS X v10.5 and iOS 3.2开始 ...
- SimpleDateFormat()简单了解
比如:SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM天dd日 HH时mm分ss秒 一年中的第 D 天 一年中第w个星期 一月中第 ...