数组中重复的数字

题目要求

在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

算法分析

题目很简单,但也出现在华为一面中,直接map存储,有重复的直接返回。

代码

class Solution(object):
    def findRepeatNumber(self, nums):
        dicts = {}
        for i in nums:
            if i not in dicts:
                dicts[i] = 1
            else:
                return i
posted @ 2020-03-24 23:49  isshpan  阅读(107)  评论(0)    收藏  举报