【题解】【位操作】【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 ...
随机推荐
- UESTC 2016 Summer Training #1 Div.2
最近意志力好飘摇..不知道坚不坚持得下去.. 这么弱还瞎纠结...可以滚了.. 水题都不会做.. LCS (A) 水 LCS (B) 没有看题 Gym 100989C 水 1D Cafeteria ( ...
- [原创]Keys的基本操作总结,判断Keys中是否存在Keys.Control|Keys.Alt,移除Keys中的部分键值。
直接看应用实例 /// <summary> /// 组合键转换成字符串类型 /// </summary> /// <param name="keyCode&qu ...
- C++ STL pair
没有找到priority_queue里存放pair不用typedef的方法...大概第一次觉得这个有用吧... 优先队列里和sort函数对pair 的默认排序是first从小到大,second从小到大 ...
- WEBService动态调用代码
BasicHttpBinding bind = new BasicHttpBinding(); bind.MaxReceivedMessageSize = int.MaxValue; Endpoint ...
- C#.NET SQL数据库备份与还原解决方案
C#.NET SQL数据库备份与还原解决方案http://www.csframework.com/archive/1/arc-1-20110924-1841.htm 开发框架V2.2(快速开发版)系统 ...
- (BFS)hdoj1242-Rescue
题目地址 初学BFS,第一次用BFS做题.题目就是一个基本的BFS模型,需要稍加注意的是遇到警卫时间要+1,以及最后比的是最短的时间而不是步数. #include<cstdio> #inc ...
- Windows8 10设置程序为 系统默认浏览器
从win8 开始,MS修改了文件和协议的关联方式,普通的注册表修改是无效的. 必须使用组策略(group policy )对象GP才行. http://blogs.technet.com/b/mrml ...
- 端午小长假--前端基础学起来03CSS为网页添加样式
定义:用于定义HTML内容在浏览器内的显示样式,如文字大小,颜色,字体 设置样式:将要设置样式的内容用<span></span>样式括起来,然后再head中设置span < ...
- typedef定义函数类型或函数指针
转载请标明出处: 最近在看redis的代码,发现了有关函数指针的部分,想把它记下来. 在redis中有类似下面的定义,利用typedef 定义了一个新的类型,这种类型是一个函数: typedef vo ...
- equals() 与 hashcode() 的区别与联系
两者都是从Object类继承的方法,Object中equals方法比较的是this和参数传进来的对象的引用地址是否相同,这样的话,equals返回值为true的必要充分条件就是两者指向同一个对象,那么 ...