Old Wei wins the offer to take you to learn --- Brush title series (array 40. The number appears only once)

40. The first digital array appear only

problem:

In addition to an array of integers in two numbers, the other numbers appear twice. Please write a program to find these two figures appear only.

solve:

thought:

This question is still counting problems, decisive use of the dictionary policy.

python code:

# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        if not array:
            return 0
        dic={}
        for x in array:
            if(dic.get(x)==None):
                dic[x]=1
            else:
                dic[x]+=1
            
        result=[]
        for i in dic:
            if(dic[i]==1):
                result.append(i)
                
        return result[0],result[1]
                
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105041300