码迷,mamicode.com
首页 > 其他好文 > 详细

First Missing Positive

时间:2018-03-16 23:46:32      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:positive   post   osi   amp   and   solution   div   body   down   

题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space.

方案:

class Solution:
 def firstMissingPositive(self, nums):
     """
     :type nums: List[int]
     :rtype: int
     """
     if nums == []:
         return 1
     M = max(nums)
     for i in range(1,M):
         if i not in nums:
             return i
     return M + 1

First Missing Positive

标签:positive   post   osi   amp   and   solution   div   body   down   

原文地址:https://www.cnblogs.com/ping1994/p/8586260.html

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