forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst_Missing_Positive.py
37 lines (31 loc) · 965 Bytes
/
First_Missing_Positive.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
37
"""
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
"""
class Solution:
# @param A, a list of integers
# @return an integer
def firstMissingPositive(self, A):
N = len(A)
i = 0
while i < N:
if A[i] <= 0 or A[i] == i + 1 or A[i] > N:
i += 1
else:
x = A[i]
if A[i] == A[x-1]:
i += 1
continue
A[i], A[x-1] = A[x-1], A[i]
for i in range(N):
if A[i] != i + 1:
return i + 1
return N + 1
# Note details
# 1. Good way to do this is name A[i] = x
# 2. line 22 need to check if it's already equal, like [1,1] will cause dead loop
# 3. line 29, return i+1 not A[i]
# 4. line 31 return N + 1