Given an 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?
這個題目還是挺簡單的,關鍵使用到了位或運算符 ^
int xor =0;
for(int i:nums){
xor^=i;
}
return xor;
這樣性能應該是最好的。
突然做到這個題目又想了一下,因爲自己對位運算符使用比較生疏,然後突然發現真的好用,僅僅四行代碼就解決了,因此開發者要學會用更多靈活的方式解決問題。