problem
- 給定一維整數陣列,找出那個single number,假設其他數字皆會出現兩次
solution
option 1 - hash table
1 | class Solution { |
set/map find operation better than count operation, find 找到就return ,count則會繼續拜訪。
option 2 - Bit Manipulation
- 善用
x^x=0
特性 以及 XOR交換率
1 | class Solution { |
analysis
- time complexity
O(n)
- space complexity
O(1)