-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path041FirstMissingPositive.py
More file actions
70 lines (62 loc) · 1.42 KB
/
041FirstMissingPositive.py
File metadata and controls
70 lines (62 loc) · 1.42 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
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.
"""
"""
Comments
Fast 用了一些修改原始数组的方法,没看懂
"""
"""
My
"""
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 1
x, n = max(nums), min(nums)
res = 1
if n > 1:
return 1
else:
i = 1
while i < x: # 这个地方直接用range的话可能会内存溢出,因为可能会出现超过范围的整数
if i not in nums:
return i
i += 1
return max(1, x+1)
"""
Fast
"""
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
for v in nums:
temp = v
while(temp <= n and temp > 0):
t1 = nums[temp - 1]
nums[temp - 1] = temp
if temp == t1:
break
temp = t1
for i,v in enumerate(nums):
if i + 1 != v:
return i + 1
return n + 1