首页 > 其他 > 详细

First Missing Positive

时间:2018-03-16 23:26:30      阅读:215      评论:0      收藏:0      [点我收藏+]

题目: 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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!