leetcode 136 python

来源:互联网 发布:跟团游推荐 知乎 编辑:程序博客网 时间:2024/05/21 05:39

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?




class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """                     s = set(nums)        l = list(s)        str1 = str(nums)        for x in l:            if str1.count(str(x)) == 1:                return x        

class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """               a = len(nums)        if a == 1:            return nums[0]                sum = 0        for x in nums:            sum = sum ^ x                  return sum        

0 0