码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode First Missing Positive python

时间:2015-12-21 23:44:36      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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

 

leetcode First Missing Positive python

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5065194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!