LeetCode 137 Single Number II
2017-07-01 11:47
441 查看
1.题目
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
给你一个数组,里边的元素都出现了三次除了一个,找出这个独一无二的元素。
2.大概思路
这个可以看作Single Number的升级版,网上有很多simple的解法,这里不提了,discuss里边有这么一个解法https://discuss.leetcode.com/topic/34725/my-own-explanation-of-bit-manipulation-method-might-be-easier-to-understand/2
研究了一下觉得很厉害。思路是这样的,把数组中的数字都用二进制表示,如果一个数字出现了三次,那么每一位的1加起来最后一定可以被3整除。如果哪一位不能被3整除,那么就是独一无二的数字。举例:2,3,2,3,2,3,1.用二进制表示如下:
2:010
3:011
2:010
3:011
2:010
3:011
1:001
count:0 6 4,分别%3,结果是0 0 1.那么独一无二的数是1.
网上很多解法都是这个思路,基于比特位统计。而这个解法是基于逻辑运算。首先我们可以知道count的每一位只有三种可能0 1 2,用二进制表示就是00 01 10,那么可以用两个数组来分别表示count的两位数,比如上边例子的结果001就可以拆成000,001.为什么这样表示呢,因为二进制没有2啊。count的具体情况可以枚举出来。假设两个数字用two和one表示,我们来任意算一位,假设当前输入位是B。
先算 one,因为one会影响two。假设计算后的结果是one‘和two’。discuss中用的two+和one+,这个容易和逻辑或混淆。
two one B one‘
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 0
这个表怎么得到,以第四行为例,01+1=11,11就是3,3%3=00,所以one位是0.为什么two one没有11呢,因为这是3啊。
然后进行逻辑化简,这个时候需要卡诺图。不知道的看这个链接http://www.elecfans.com/book/story.php?id=511。学过数字逻辑的应该有印象。
得到如下卡诺图:
然后计算two’,这个时候我进入了一个误区,通过two one B计算two’,事实上one已经是one’了,因此应该用two one’ B计算two’
卡诺图如下:
最后得到
one’ =(~two)& (one ^ B)
two’ = (~one’) & (two ^ B)
最后返回one’就行了,为什么只要返回one’而没有two‘的事呢。因为我们只要返回%3=1的结果就行了,也就是0 1,而%3=0是00 %3=2是10,因此只要返回one就行了。简单说one=1是%3=1的充要条件。
3.代码
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
给你一个数组,里边的元素都出现了三次除了一个,找出这个独一无二的元素。
2.大概思路
这个可以看作Single Number的升级版,网上有很多simple的解法,这里不提了,discuss里边有这么一个解法https://discuss.leetcode.com/topic/34725/my-own-explanation-of-bit-manipulation-method-might-be-easier-to-understand/2
研究了一下觉得很厉害。思路是这样的,把数组中的数字都用二进制表示,如果一个数字出现了三次,那么每一位的1加起来最后一定可以被3整除。如果哪一位不能被3整除,那么就是独一无二的数字。举例:2,3,2,3,2,3,1.用二进制表示如下:
2:010
3:011
2:010
3:011
2:010
3:011
1:001
count:0 6 4,分别%3,结果是0 0 1.那么独一无二的数是1.
网上很多解法都是这个思路,基于比特位统计。而这个解法是基于逻辑运算。首先我们可以知道count的每一位只有三种可能0 1 2,用二进制表示就是00 01 10,那么可以用两个数组来分别表示count的两位数,比如上边例子的结果001就可以拆成000,001.为什么这样表示呢,因为二进制没有2啊。count的具体情况可以枚举出来。假设两个数字用two和one表示,我们来任意算一位,假设当前输入位是B。
先算 one,因为one会影响two。假设计算后的结果是one‘和two’。discuss中用的two+和one+,这个容易和逻辑或混淆。
two one B one‘
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 0
这个表怎么得到,以第四行为例,01+1=11,11就是3,3%3=00,所以one位是0.为什么two one没有11呢,因为这是3啊。
然后进行逻辑化简,这个时候需要卡诺图。不知道的看这个链接http://www.elecfans.com/book/story.php?id=511。学过数字逻辑的应该有印象。
得到如下卡诺图:
然后计算two’,这个时候我进入了一个误区,通过two one B计算two’,事实上one已经是one’了,因此应该用two one’ B计算two’
卡诺图如下:
最后得到
one’ =(~two)& (one ^ B)
two’ = (~one’) & (two ^ B)
最后返回one’就行了,为什么只要返回one’而没有two‘的事呢。因为我们只要返回%3=1的结果就行了,也就是0 1,而%3=0是00 %3=2是10,因此只要返回one就行了。简单说one=1是%3=1的充要条件。
3.代码
1 2 3 4 5 6 7 8 | public int singleNumber(int[] nums) { int one = 0, two = 0; for (int i = 0; i < nums.length; i++) { one = (~two) & (one ^ nums[i]); two = (~one) & (two ^ nums[i]); } return one; } |
相关文章推荐
- Java [Leetcode 137]Single Number II
- LeetCode 137 Single Number II
- The Solution to Leetcode 137 Single Number II
- LeetCode 137 Single Number II
- [Leetcode 137, Medium] Single Number II
- LeetCode(137)Single NumberII
- LeetCode 137 Single Number II 解题报告
- leetCode: Single Number II [137]
- 【leetcode】[137]Single Number II
- [LeetCode 137] Single Number II(位运算的巧妙运用初级篇)
- LeetCode 137 Single Number II
- LeetCode 137. Single Number II
- [LeetCode]137 Single Number II
- Leetcode 137 Single Number II
- [leetcode-137]Single Number II (c)
- 【LeetCode-面试算法经典-Java实现】【137-Single Number II(只字出一次的数字II)】
- [LeetCode]题解(python):137-Single Number II
- leetcode——137—— Single Number II
- 137. Single Number II LeetCode
- LeetCode(136)(137) Single Number I II