Single Number 系列
经典题目
a .length == n, one appear once and others appear 2 times. Find the one.
异或消除法:a ^ a = 0
// T/O(n), S/O(1)
public int singleNumber(int[] nums) {
int ans = 0;
for (int num : nums) {
ans ^= num;
}
return ans;
}
a.length == n, one appear once and others appear 3 times. Find the one.
// T/O(n), S/O(1)
a.length == n, two appear once and others appear 2 times. Find the one.
// T/O(n), S/O(1)
Last updated