- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path41.py
36 lines (30 loc) · 859 Bytes
/
41.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
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
'''
classSolution(object):
deffirstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
index_i=0
forindex_jinrange(len(nums)):
ifnums[index_j] <=0:
nums[index_i], nums[index_j] =nums[index_j], nums[index_i]
index_i+=1
forindexinrange(index_i, len(nums)):
ifabs(nums[index]) -1<len(nums) andnums[abs(nums[index]) -1] >0:
nums[abs(nums[index]) -1] =-nums[abs(nums[index]) -1]
forindexinrange(nums):
ifnums[index] >0:
returnindex+1
returnlen(nums) +1