[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的解法,但是思路可以向逻辑运算符那边靠。这题我开始没有想出来,这个逻辑运算对我而言是一个大坑。看了Grandyang的博客才明白。这里给出原博客中的解释:用3个整数来表示INT的各位的出现次数情况,one表示出现了1次,two表示出现了2次。当出现3次的时候该位清零。最后答案就是one的值,
ones 代表第ith 位只出现一次的掩码变量;twos 代表第ith 位只出现两次的掩码变量;threes 代表第ith 位只出现三次的掩码变量。
博主的个人理解经验是:写出一个例子,按照程序走一遍。
 class Solution {
 public:
     int singleNumber(int A[], int n)
     {
         int one=,two=,three=;
         for(int i=;i<n;++i)
         {
             two |=one&A[i];
             one ^=A[i];
             three=one&two;
             one &=~three;
             two &=~three;
         }
         return one;
     }
 };
原博客中还给出了Single Number III
[Leetcode] single number ii 找单个数的更多相关文章
- 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 单独的数字之二
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - LeetCode:Single Number II
		
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
 - [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
		
题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...
 - Leetcode Single Number II (面试题推荐)
		
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
 - leetcode Single Number II - 位运算处理数组中的数
		
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
 - LeetCode——Single Number(找出数组中只出现一次的数)
		
问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...
 - LeetCode | Single Number II【转】
		
题目:Given an array of integers, every element appears three times except for one. Find that single on ...
 
随机推荐
- 生鲜水果商城PC手机微信完整版源码2018版(免费)
			
采用php+mysql架构,含有PC.手机.微信三端,只需要修改一下数据库配置,并恢复一下数据即可使用,还有微信.支付宝等接口,如有问题请在文章下面留言一下,我看到会协助一下的,下载包里面含有详细的安 ...
 - 第五模块:WEB开发基础 第3章·BootStrap&JQuery开发
			
01-JQuery介绍 02-jQuery文件引入和加载的区别 03-jQuery的基础选择器 04-jQuery的层级选择器 05-jQuery的基本过滤选择器 06-jQuery的属性选择器 07 ...
 - 【WXS】变量定义保留标识符
			
以下字符不能作为变量名称定义: delete void typeof null undefined NaN Infinity var if else true false require this f ...
 - 从零开始的Python学习Episode 5——字典
			
字典 字典是另一种可变容器模型,且可存储任意类型对象. 一.添加 (1)直接添加 dict={'name':'smilepup'} dict['age']=20 dict['name']='piggy ...
 - 七:Web Application Proxy
			
yarn自带了web接口,默认是和RM一起的(8088端口).但是为了减少从web接口受到的攻击,可以把Web接口单独放在别的机器上. 设置下web代理就行了 Configurations Confi ...
 - 二 Capacity Scheduler 计算能力调度器
			
官网的写的太难懂,参考:http://www.360doc.com/content/14/0603/14/14935022_383254798.shtml Capacity Scheduler 一种可 ...
 - ZOJ 2760 How Many Shortest Path(最短路径+最大流)
			
Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...
 - LVS+Keepalive+Nginx实现负载均衡
			
本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...
 - 20162328蔡文琛week09
			
学号 2016-2017-2 <程序设计与数据结构>第X周学习总结 教材学习内容总结 数据库是为了其他程序提供数据的应用软件. 关系书就哭通过唯一的标识符在不同表的记录见建立了关系. JD ...
 - c#事件实质
			
c#的事件实际上是对windows消息的封装: windows消息系统分为3部分:消息队列,消息循环,窗口过程(wndproc函数)