码迷,mamicode.com
首页 > 编程语言 > 详细

数组中只出现一次的数字-python

时间:2019-08-10 14:11:37      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:pytho   次数   HERE   find   write   append   python   sel   个数   

思路:用一个字典统计出每个数字出现的次数,然后遍历字典,找到只出现一次的

# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        dic = {}
        for i in array:
            if i in dic:
                dic[i] += 1
            else:
                dic[i] = 1
        res = []
        for i in dic:
            if dic[i] == 1:
                res.append(i)
        return res

数组中只出现一次的数字-python

标签:pytho   次数   HERE   find   write   append   python   sel   个数   

原文地址:https://www.cnblogs.com/dolisun/p/11331294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!