-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0041_first-missing-positive.py
More file actions
38 lines (29 loc) · 1.02 KB
/
0041_first-missing-positive.py
File metadata and controls
38 lines (29 loc) · 1.02 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
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
for i in range(len(nums)):
if nums[i] <= 0:
nums[i] = 0
for i in range(len(nums)):
check = abs(nums[i])
if 1 <= check <= len(nums):
if nums[check - 1] > 0:
nums[check - 1] *= -1
elif nums[check - 1] == 0:
nums[check - 1] = -1 * (len(nums) + 1)
for i in range(1, len(nums)+1):
if nums[i-1] >= 0:
return i
return len(nums) + 1
# if all(num <= 0 for num in nums):
# return 1
# nums[:] = [num for num in nums if num > 0]
# if len(nums) != 0:
# maximum = max(nums)
# else:
# return 1
# for num in range(1, maximum+1):
# if num not in nums:
# return num
# else:
# continue
# return maximum + 1