tag: python3 、 数组
给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
输入: [1,2,0]
输出: 3
输入: [3,4,-1,1]
输出: 2
输入: [7,8,9,11,12]
输出: 1
使用 collections.Counter
函数构造哈希表统计数组中各个数字出现的情况,输出第一个出现次数为零的数字。
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
import collections
t = collections.Counter(nums)
i = 1
while 1:
if t[i] == 0:
return i
i += 1
最终结果,运行时间36ms,超过85.58%;占用内存13.7MB,超过16.67%。