Single Number
easy 原题链接:https://leetcode.com/problems/single-number/
最后更新于
这有帮助吗?
easy 原题链接:https://leetcode.com/problems/single-number/
最后更新于
这有帮助吗?
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?
Input: [2,2,1]
Output: 1
2∗(a+b+c)−(a+a+b+b+c)=c
Concept
If we take XOR of zero and some bit, it will return that bit
a ⊕0 = a
If we take XOR of two same bits, it will return 0
a⊕a=0
a⊕b⊕a=(a⊕a)⊕b=0⊕b=b
So we can XOR all bits together to find the unique number.