数组中重复的数字
题目要求
在一个长度为 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

浙公网安备 33010602011771号