美文网首页剑指offer-python
数组中只出现一次的数字

数组中只出现一次的数字

作者: fighting_css | 来源:发表于2018-08-29 11:59 被阅读0次

思路:建立字典,遍历,时间复杂度O(n)
代码:

class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        dict_map = {}
        res = []
        if not array or len(array)<0:
            return res
        n = len(array)
        for i in range(n):
            if array[i] in dict_map.keys():
                dict_map[array[i]] +=1
            else:
                dict_map[array[i]]=1
        for key in dict_map.keys():
            if dict_map[key]==1:
                res.append(key)
        return res

相关文章

网友评论

    本文标题:数组中只出现一次的数字

    本文链接:https://www.haomeiwen.com/subject/ozmhwftx.html