【leetcode】Single Number II (medium) ★ 自己没做出来....
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?
自己不会做... 搜答案,发现思路真是太巧妙了
关键:统计每一位上出现1的次数!!! 可以对每一位统计,也可以对期望出现的次数统计。
答案来自:http://blog.csdn.net/jiadebin890724/article/details/23306837
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;
}
};
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;
}
if(n% == )
{
return one;
}
else if(n% == )
{
return two;
}
}
};
【leetcode】Single Number II (medium) ★ 自己没做出来....的更多相关文章
- 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 找单个数
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 单元素2
题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
随机推荐
- 解决pydev无法增加jython271 interpreter的问题
============================解决pydev无法增加jython271 interpreter的问题============================ 从jython. ...
- ie8及其以下浏览器的document.getElementsByClassName兼容性问题
使用JavaScript访问DOM的一个重大问题是,此过程需要一种通过元素类名称来选择类的类函数,对DOMContentReady,这种类函数的缺失导致开发人员需要自己编写自定义脚本业执行上述任务,许 ...
- 使用Minify来优化网站性能
Minify 是用PHP5开发的应用,通过遵循一些Yahoo的优化规则来提高网站的性能.它会合并多个CSS或者JavaScript文件,移除一些不必要的空格和注释,进行gzip压缩,并且会设置浏览器的 ...
- flexbox-CSS3弹性盒模型flexbox完整版教程
原文链接:http://caibaojian.com/flexbox-guide.html flexbox-CSS3弹性盒模型flexbox完整版教程 A-A+ 前端博客•2014-05-08•前端开 ...
- Bellman_Ford
Source:http://www.cnblogs.com/Jason-Damon/archive/2012/04/21/2460850.html 摘自百度百科 Bellman-ford算法是求含负权 ...
- Hello,World
引用自http://www.cnblogs.com/jbelial/archive/2013/05/08/3067471.html#2676127 题目: 1 public class text { ...
- CodeVS 1344 线型网络
Sol 随机化算法+哈密顿路径. 好厉害的题...首先都会想到状压DP对吧,复杂度 \(O(n^2 2^n)\) . \(n=20\) exm?? \(10^8\) 有一种算法就是随机化算法 再调整 ...
- Python自动化之YAML解析
准备工作 pip install PyYAML import yaml yaml语法规则 想要表示列表项,使用一个短横杠加一个空格.多个项使用同样的缩进级别作为同一列表的一部分 my_dictiona ...
- iOS_隐藏顶部状态栏方式
关键词:IOS.UIViewController. Status Bar iOS6和iOS7在隐藏 Status Bar 三种方式比较: Storyboard 界面上选中UIViewControlle ...
- 注册页面的js验证
简单的用户注册页面:(html) 包含用户名格式验证.邮箱格式验证.确认密码一致性验证和必填项验证.(纯javascript) <center> <h1>用户注册</h1 ...