边工作边刷题:70天一遍leetcode: day 11-3

Single Number I/II

II的python解是网上抄的,其实可以AC,但是python不会像c++/java那样自动overflow,而是转化成long。所以如果有负数的情况会得到一个巨大的正数解,比如

Input:[-2,-2,1,1,-3,1,-3,-3,-4,-2]

Output:4294967292

Expected:-4

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = 0
        for i in nums:
            res = i ^ res
        
        return res
原文地址:https://www.cnblogs.com/absolute/p/5675812.html