【leetcode】136. Single Number

来源:互联网 发布:形式与政策 网络强国 编辑:程序博客网 时间:2024/04/28 06:33

single number系列问题


single number Ⅰ

要求O(n)的时间复杂度,也就是只能有一层循环,于是想到对list里的每个数字建立字典统计词频。

class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """        dict  = {}        for each in nums:            if each in dict:                dict[each] += 1            else:                dict[each] = 1        for i in dict:            if dict[i]==1:                return i         else:            return None            

single number Ⅱ

要求不再使用更多的内存,不能建立新的词典。那就使用原有list的内存...

0 0
原创粉丝点击