leetcode First Missing Positive python

时间:2023-03-10 02:22:49
leetcode First Missing Positive python
class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
intlen=len(nums)
i=0
while i < intlen:
if nums[i] > 0 and nums[i] < intlen and nums[i] != nums[nums[i]-1]:
          #get the current num, and put it into it's right place
#for example : if get the third item is 5 ( index is 2, start with 0 ) , so the index of 5 should be 4
tmp=nums[nums[i]-1]
nums[nums[i]-1]=nums[i]
nums[i]=tmp
else:
i+=1 for i in range(intlen):
if nums[i] != i+1:
return i+1 return intlen+1