Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
1 |
Input: [2,2,1] |
Example 2:
1 |
Input: [4,1,2,1,2] |
O(1)time O(n)space
先将所有数字存入hash table,key是数字,value是数字的个数。
1 |
int (vector<int>& nums) { |
Bit manipulation
因为^运算具有交换律,且^运算同一个数结果为0,所以将所有数累计异或就可以得到只出现一次的数。
1 |
int (vector<int>& nums) { |
近期评论