题目
Given an array of integers, every element appears three timesexcept for one. Find that single one.
其他出现3次,找出唯一出现1次的
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路(别人的)
I'll try to explain ziyihao solution since the first answer is not accurately explained. The main idea of ziyihao's solution is coming from course digital logic. That is, we have a initial state(x, y), and a target state(z), the goal is to find out the binary operations(z=f(x, y)):
The solution use two bit (a,b) to represent the initial state:
- 00: appear 3n times
- 01: appear 3n+1 times
- 10: appear 3n+2 times
The target state can be easily deducted that we want:
if data incoming: 00 -> 01 ->10 -> 00 if no data incoming, no change.
As a result, we can calculate the table in ziyihao's post:
current incoming next
a b c a b
0 0 0 0 0
0 1 0 0 1
1 0 0 1 0
0 0 1 0 1
0 1 1 1 0
1 0 1 0 0
The next step is to calculate the binary operations we need from this state table, which is actually a general technique used in digital circuit course. Let's look at bit "a" first:
current incoming next
a b c a
0 0 0 0
0 1 0 0
1 0 0 1
0 0 1 0
0 1 1 1
1 0 1 0
As mentioned in ziyihao's post, we only need care about the rows has value "1" in "next " column, that is row 3 and row 5.
So we can get:
a=a&~b&~c + ~a&b&c
Use the same technique and we can get the binary operations for b.
代码
int singleNumber(int* nums, int numsSize) {
int a=0;
int b=0;
int c;
int i;
//we need to implement a tree-time counter(base 3) that if a bit appears three time ,it will be zero.
//#curent income ouput
//# ab c/c ab/ab
//# 00 1/0 01/00
//# 01 1/0 10/01
//# 10 1/0 00/10
// a=~abc+a~b~c;
// b=~a~bc+~ab~c;
for(i=0;i<numsSize;i++)
{
c = nums[i];
int tmp = (~a&b&c)|(a&~b&~c);
b = (~a&~b&c)|(~a&b&~c);
a = tmp;
}
// //we need find the number that is 01,10 => 1, 00 => 0.
return a|b;
}
最重要的是推出那个公式!!!
int tmp = (~a&b&c)|(a&~b&~c);
b = (~a&~b&c)|(~a&b&~c);
a = tmp;设置tmp变量是因为,计算b的时候需要的a是上一状态,而不是当前状态
本文探讨了如何在数组中找到只出现一次的数字,所有其他数字都出现三次。通过利用数字逻辑运算,实现O(n)时间复杂度且不使用额外内存的方法。
412

被折叠的 条评论
为什么被折叠?



