Leetcode Single Number II (面试题推荐)
还记得《剑指offer》和《编程之美》等书上多次出现的找一个数组中仅仅出现一次的数那个题吗?
leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复杂度内求出的方法,这里不再赘述。
以下就是上题的升级版
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?
给你一个整数数组。每一个元素出现了三次。但仅仅有一个元素出现了一次,让你找出这个数。要求线性的时间复杂度,不使用额外空间。
这样就不能使用异或的方法了。把全部元素以后得到的就是全部元素的异或值了,对我们没有不论什么帮助。
这个问题依然使用位运算来解决,只是更加复杂。
我们用三个变量one two three,分别存储二进制未分别出现一次 两次 三次的位。
class Solution {
public:
int singleNumber(int A[], int n) {
int one = 0, two = 0, three = 0;
for(int i = 0; i < n; i++)
{
three = two & A[i]; //出现三次的位肯定是由出现两次的得到的
two = two | ones & A[i]; //出现两次的肯定是出现一次的得到的,只是还有原有的所以要或
one = one | A[i]; //计算出现一次的位
two = two & ~three; //去掉二进制中出现三次的位
ones = one & ~three; //去掉二进制中出现三次的位</span>
}
return one; //终于twos three都为0。one就是我们要的答案
}
};
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(找出数组中只出现一次的数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 @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [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 位运算
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 on ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
随机推荐
- Exploded location overlaps an existing deployment解决办法
项目->properties->MyEclipse->Web->Web Context-root的名字为重命名之后的名字即可
- Ext 6.5.3 classic版本,自定义实现togglefield开关控件
1,在Ext 6.5.3的classic版中没有提供开关控件,参照modern版中 togglefield开关的实现,继承滑动器(sliderfield),自定义一个开关按钮.支持value绑定和点击 ...
- poj1681 Painter's Problem
题目描述: 和那道关灯差不多,求最少涂几次. 题解: 高消,然后深搜枚举自由元更新答案. 貌似这道题没卡贪心但是其他题基本都卡了. 比如$Usaco09Nov$的$lights$ 代码: #inclu ...
- CentOS 6及7 丢失root密码解决方案
6.x系列 法一:使用光盘镜像 BIOS中设置CD-ROM启动——选择救援模式——系统被自动挂载到/mnt/sysimage下——选择进入shell start shell——进入shell命令行—— ...
- qt c++对象头文件如何相互包含
今天在写qt时,遇到了两个类相互包含的问题,类A要用到类B,类B要用到类A. 类A:a.h #ifndef A_H #define A_H #include <b.h> class A { ...
- MySQL教程之存储过程与函数
存储程序分为存储过程和函数 可以使用CALL来调用存储过程,只能输出变量返回值.存储过程可以调用其他存储过程 函数可以从语句外调用,也能返回标量值 什么是存储过程? 简单的说,就是一组SQL语句集,功 ...
- vim 系列
http://blog.csdn.net/laogaoav/article/details/17370269 非常不错
- Java学习之接口概念
Java语言只支持单重继承,不支持多继承,即一个类只能有一个父类.但是在实际应用中,又经常需要使用多继承来解决问题.为了解决该问题,Java语言提供接口来实现类的多继承问题. 接口(英文interfa ...
- python模块以及导入出现ImportError: No module named ‘xxx‘问题
python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用如果你要使 ...
- hls简述(HTTP live Streaming)
hls官方地址:https://developer.apple.com/streaming/ IDR: Instantaneous Decoding Refresh (IDR) start code ...