136 - Single Number
Written on October 21, 2015
Tweet
Given an array of integers, every element appears twice except for one. Find that single one.
public class Solution {
//利用位运算中的异或:x^x = 0, x^0 = x。并且异或有交换律:1^1^0 = 0 = 1^0^1
public int singleNumber(int[] A) {
int result = 0;
for (int n : A) {
result ^= n;
}
return result;
}
}
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
xor = 0
for num in nums:
xor ^= num
return xor