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?
給一非空陣列的整數陣列,每個元素都會出現兩次,只有一個元素會出現一次。找到孤單的那個傢伙。
注意:
你的演算法應該是O(n)的複雜度,你可以不用額外的記憶體空間實作嗎?
hash-table
,有元素就加到集合內,已經有的就踢掉,最後集合內只會剩下一個數字。XOR
邏輯解,我們看看以下式子:
A XOR 0 = A
A XOR A = 0
LeetCode
C++