[LeetCode] Single Number

问题描述:

  Given an array of integers, every element appears twice except for one. Find that single one.

每个元素出现两次,主要考察异或运算。一开始考虑到两两相消,但忘记存储时是用01存储,可以用异或相消。

class Solution:
    # @param A, a list of integer
    # @return an integer
    def singleNumber(self, A):
        result = 0
        for i in A:
            result = result^i
        return result

你可能感兴趣的:(LeetCode)