【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 ...
随机推荐
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...
- [译]git commit --amend
git commit --amend命令用来修复最近一次commit. 可以让你合并你缓存区的修改和上一次commit, 而不是提交一个新的快照. 还可以用来编辑上一次的commit描述. 记住ame ...
- 论在Windows下远程连接Ubuntu
Ubuntu下1:下载xrdp sudo apt-get install xrdp 2: urs/share/applications 下找到 远程桌面 设置成这样 Windows下 1; ...
- POJ 1905 Expanding Rods
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- js做灯泡明灭特效
W3school中的js专讲这一块 http://www.w3school.com.cn/tiy/t.asp?f=js_lightbulb
- PHP 三元运算符省略写法
三元运算符 “?:” 又名条件运算符 表达式 (expr1) ? (expr2) : (expr3) 在 expr1 求值为 TRUE 时的值为 expr2,在 expr1 求值为 FALSE 时的值 ...
- Javascript高级程序设计——执行环境与作用域
Javascript中执行环境是定义了变量或函数有权访问的其他数据,决定了各自的行为,每个执行的环境都有一个与之关联的变量对象,环境中定义的所以变量和函数都保存在这个对象中. 全局执行环境是最外围的一 ...
- Knockout.Js案例三单页面应用程序
<ul data-bind="foreach: folders"> <li data-bind="text: $data">& ...
- BZOJ1146——[CTSC2008]网络管理Network
1.题目大意:就是在动态的树上路径权值第k大. 2.分析:这个就是树链剖分+树套树 #include <cstdio> #include <cstdlib> #include ...
- 真有用?Snap和Flatpak 通吃所有发行版的打包方式。
导读 最近我们听到越来越多的有关于Ubuntu的Snap包和由Red Hat员工Alexander Larsson创造的 Flatpak (曾经叫做 xdg-app)的消息.这两种下一代打包方法在本质 ...