forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst-missing-positive.py
More file actions
31 lines (28 loc) · 838 Bytes
/
first-missing-positive.py
File metadata and controls
31 lines (28 loc) · 838 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
27
28
29
30
31
# Time: O(n)
# Space: O(1)
#
# 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):
i = 0
while i < len(A):
if A[i] > 0 and A[i] - 1 < len(A) and A[i] != A[A[i]-1]:
A[A[i]-1], A[i] = A[i], A[A[i]-1]
else:
i += 1
for i, integer in enumerate(A):
if integer != i + 1:
return i + 1
return len(A) + 1
if __name__ == "__main__":
print Solution().firstMissingPositive([1,2,0])
print Solution().firstMissingPositive([3,4,-1,1])