forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (23 loc) · 672 Bytes
/
solution.py
File metadata and controls
26 lines (23 loc) · 672 Bytes
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
"""
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:
j = A[i] - 1
if A[i] != i + 1 and A[i] >= 1 and A[i] <= n and A[i] != A[j]:
A[i], A[j] = A[j], A[i]
else:
i += 1
for i, e in enumerate(A):
if e != i + 1:
return i + 1
return n + 1