Skip to content

Commit 01fae6c

Browse files
Create FirstMissingPositive.py
1 parent 4b9f145 commit 01fae6c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: python/FirstMissingPositive.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
https://leetcode.com/problems/first-missing-positive/
3+
Given an unsorted integer array nums, return the smallest missing positive integer.
4+
"""
5+
class Solution(object):
6+
def firstMissingPositive(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
i=0
12+
while i<len(nums):
13+
correct=nums[i]-1
14+
if nums[i]>0 and correct<len(nums) and nums[i]!=nums[correct]:
15+
nums[i],nums[correct]=nums[correct],nums[i]
16+
else:
17+
i+=1
18+
flag=0
19+
for j in range(0,len(nums)):
20+
if nums[j]-1!=j:
21+
return j+1
22+
return len(nums)+1

0 commit comments

Comments
 (0)