[OJ] Single Number II
LintCode 83. Single Number II (Medium)
LeetCode 137. Single Number II (Medium)
以下算法的复杂度都是:
时间复杂度: O(n)
空间复杂度: O(1)
解法1. 32个计数器
最简单的思路是用32个计数器, 满3复位0.
class Solution {
public:
int singleNumberII(vector<int> &A) {
int cnt[32] = {0};
int res = 0;
for (int i = 0; i < 32; ++i) {
for (int n : A) {
cnt[i] += (n >> i) & 1;
cnt[i] %= 3;
}
res |= cnt[i] << i;
}
return res;
}
};
解法2. 找规律
我的思路是, 解法1中的计数其实只需要两个bit就够了. 所有的首个bit记做res, 所有的第二个bit记做carry, 找规律:
如果A[i]的第k位是0, 则res和carry的第k位保持原样.
如果A[i]的第k位是1, 则:
| res | carry | res' | carry' |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 |
此时(A[i][k]=1时)的规律就是:
res'[k]=~res[k] & ~carry[k]
carry'[k]=res[k] & ~carry[k]
class Solution {
public:
int singleNumberII(vector<int> &A) {
int res = 0, carry = 0;
for (int n : A) {
for (int i = 0; i < 32; ++i) {
int mask = (1 << i);
int bit = n & mask;
if (bit) {
int newRes = (~mask & res) + ((~res & mask) & (~carry & mask));
carry = (~mask & carry) + ((res & mask) & (~carry & mask));
res = newRes;
}
}
}
return res;
}
};
解法2.1. 解法2的简化
解法2中, 将A[i][k]=0和=1的情况合并, 可以得到:
res'[k]=(A[i][k] & ~res[k] & ~carry[k]) | (~A[i][k] & res[k])
carry'[k]=(A[i][k] & res[k] & ~carry[k]) | (~A[i][k] & carry[k])
这样的好处是可以32位一起算, 而不用一位一位地算:
res'=(A[i] & ~res & ~carry) | (~A[i][k] & res)
carry'=(A[i] & res & ~carry) | (~A[i][k] & carry)
class Solution {
public:
int singleNumberII(vector<int> &A) {
int res = 0, carry = 0;
for (int n : A) {
int newRes = (n & (~res & ~carry)) | (~n & res);
carry = (n & (res & ~carry)) | (~n & carry);
res = newRes;
}
return res;
}
};
解法3. one, two, three
Discuss中看到的解法, 自己实在想不出来. 用one, two和three三个int值作为bit flags.
循环对A[0]到A[n]进行考察, 当考察A[i]时:
记S[i]={A[0],...,A[i]},
若S[i]中所有数字的第k位bit的数目%3==1, 则one的第k位为1, 否则为0.
若S[i]中所有数字的第k位bit的数目%3==2, 则two的第k位为1, 否则为0.
而three是一个临时变量, 用于记录这一轮中, 哪些bit的数目恰巧是3的倍数.
two |= one & n;: 给two加上那些从1到2的数字.
one ^= n;: 这句比较巧妙, 既删掉了会变成2的那些1, 又加上了新的1.
three = one & two;: 1+2=3...
one&= ~three: 从1中刨去那些成为3的1.
two&= ~three: 从2中跑去那些成为3的2.
...如果你能解释得更清晰易懂, 欢迎留言!
class Solution {
public:
int singleNumberII(vector<int> &A) {
int one = 0, two = 0, three = 0;
for (int n : A) {
two |= one & n;
one ^= n;
three = one & two;
one &= ~three;
two &= ~three;
}
return one;
}
};
解法3.1. 解法3的变形
自己没想出解法4, 但是参照它的思路, 写了一个对自己比较直观的算法.
three = two & n;: 算出从2变成3的那些2.
two = (two & ~three) | (one & n);: (two & ~three)是从2中刨去那些变为3的2, (one & n)是加上那些从1变成2的1.
one = (one & ~n) | (n & ~three & ~two);: (one & ~n)是从1中刨去那些变成2的1, (n & ~three & ~two)是加上那些没给"2变3"或"1变2"用过的1.
class Solution {
public:
int singleNumberII(vector<int> &A) {
int one = 0, two = 0;
for (int n : A) {
int three = two & n;
two = (two & ~three) | (one & n);
one = (one & ~n) | (n & ~three & ~two);
}
return one;
}
};
[OJ] Single Number II的更多相关文章
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Single Number,Single Number II
Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...
- leetcode 之 Single Number II
问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
随机推荐
- 杂技之sharpdevelop调试aps.net
背景: 本人笔记本电脑不给力,vs打开实在太慢,因此考虑使用sharpdevelop,但sharpdevelop有点麻烦事,就是不支持asp.net的调试,为解决此问题,本人在此杂技一把了 方案一: ...
- tomcat maxConnections和maxThreads区别
maxConnections:与tomcat建立的最大socket连接数,默认10000(很多网上说200,实际上通过tomcat7.0.55源码查看可以知道是10000),AbstractEndpo ...
- 11_Jaxws常用注解
[不使用注解] 默认namespace是服务类包名的倒序 默认portType是服务类的类名 ............... 注解的所起的作用: Jaxws提供的注解可以对WebService的接口规 ...
- HDU 4768 Flyer(二分法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目大意:每组数据有n行输入,每行有三个数A.B.C,A<=B且小于2^32,从A到B每隔 ...
- sgu 104 Little Shop of Flowers
经典dp问题,花店橱窗布置,不再多说,上代码 #include <cstdio> #include <cstring> #include <iostream> #i ...
- Huffman Coding 哈夫曼编码
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4096079.html 使用优先队列实现,需要注意以下几点: 1.在使用priority_qu ...
- Git问题:Cannot update paths and switch to branch 'dev' at the same time.
使用命令 $ git checkout -b develop origin/develop 签出远程分支,出现以下错误: fatal: Cannot update paths and switch t ...
- SET ANSI_NULLS ON
Transact-SQL 支持在与空值进行比较时,允许比较运算符返回 TRUE 或 FALSE. 通过设置 ANSI_NULLS OFF 可将此选项激活.当 ANSI_NULLS 为 OFF 时,如果 ...
- Mysql zip压缩包安装
解压mysql.zip 配置环境变量(略) 配置my-default.ini 配置文件 安装mysql:mysqld -install 初始化mysql:mysqld --initialize 启动服 ...
- Oracle的rowid结构解析
SQL> select rowid,deptno from dept; ROWID DEPTNO ------------------ ---------- A ...