- 分类:Array
- 时间复杂度: O(n)
41. First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
代码:
方法:
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#如何用constant extra space不知道怎么做
#如果不用constant extra space可以取巧的做
my_nums=set(nums)
i=1
while True:
if i in nums:
i+=1
else:
return i
讨论:
1.一道array题,如果没有固定的constant extra space,这道题就变得很简单。
2.问问CY在constant extra space的情况下怎么做