【剑指Offer】40.数组中只出现一次的数字(Python实现)

题目描述

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

解法一:映射法

# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        new = {}
        for i in array:
            new[i] = 1 if i not in new else new[i] + 1
        res = []
        for k,v in new.items():
            if v == 1:
                res.append(k)
        return res
发布了70 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36936730/article/details/104691122
今日推荐