天天看點

【leetcode 簡單】第三十四題 隻出現一次的數字

給定一個非空整數數組,除了某個元素隻出現一次以外,其餘每個元素均出現兩次。找出那個隻出現了一次的元素。

說明:

你的算法應該具有線性時間複雜度。 你可以不使用額外空間來實作嗎?

示例 1:

輸入: [2,2,1]
輸出: 1
      

示例 2:

輸入: [4,1,2,1,2]
輸出: 4

      
def make():
    fil = {}
    def filter_nums(nums):
        for i in nums:
            if i not in fil:
                fil[i] =0
            else:
                fil[i] +=1
        return [i[0] for i in fil.items() if i[1] == 0][0]
    return filter_nums

class Solution:
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a=make()
        return a(nums)      

轉載于:https://www.cnblogs.com/flashBoxer/p/9484945.html