# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 41: First Missing Positive
https://oj.leetcode.com/problems/first-missing-positive/ 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. ===Comments by Dabay===
要求O(n)的时间,肯定不能常规排序,意思是只能扫描一次。
因为对空间有要求,所以只能在已经有的空间上做文章。 扫描的时候,当遇到正数,而且这个正数小于数组长度(因为如果大于数组长度,说明前面肯定缺少正数,同时也没有(无需)位置来存储它),
就把它交换到下标和它一样的位置上。
这样,扫描完成之后,从1的位置开始判断,如果位置k上存的数字不是k,这就是缺少的第一个正数。 这道题有三个需要注意的地方:
- 交换之后,下标不能移动,需要继续判断。
- 可能从1开始到最后都没有缺少的正数,此时下一个正数可能放在第一个位置上。
- 当数组长度为0时,直接返回1.
''' class Solution:
# @param A, a list of integers
# @return an integer
def firstMissingPositive(self, A):
if len(A) == 0:
return 1
i = 0
while i < len(A):
if A[i] > 0 and A[i] < len(A) and A[A[i]] != A[i]:
A[A[i]], A[i] = A[i], A[A[i]]
else:
i = i + 1
for x in xrange(1, len(A)):
if A[x] != x:
return x
else:
if A[0] == len(A):
return len(A) + 1
else:
return len(A) def main():
s = Solution()
print s.firstMissingPositive([3,4,-1,1]) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]41: First Missing Positive的更多相关文章

  1. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  2. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  3. leetcode problem 41 -- First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  4. LeetCode OJ 41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  5. 【LeetCode】41. First Missing Positive (3 solutions)

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  6. 【leetcode】41. First Missing Positive

    题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...

  7. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  8. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  9. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

随机推荐

  1. Android 圆形按钮实现

    项目中用到的圆形按钮,做个半天,用sharp形式实现,样式代码如下: <Button android:id="@+id/btn_5" android:layout_width ...

  2. 【转】Linux驱动模块编译进内核中

    原文网址:http://blog.chinaunix.net/uid-29287950-id-4573481.html BQ27501驱动编译进内核 一.       驱动程序编译进内核的步骤 在 l ...

  3. PHP代码,拒绝频繁访问

    一个网站性能有限,如果有人恶意去频繁对页面进行刷新,其实对服务器影响是很大的,导致资源使用非常高,直接影响到其他用户的体验. 那么对于这样的一些频繁访问,我们该如何去拒绝它呢? 我总结了两种方法:第一 ...

  4. Clone使用方法详解【转载】

    博客引用地址:Clone使用方法详解 Clone使用方法详解   java“指针”       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...

  5. MSSQL 常用内置函数

    一.判断表是否存在 IF objectproperty(object_id(@tableName),'IsUserTable') IS NOT NULL PRINT '存在' ELSE PRINT ' ...

  6. vs在线工具杂烩

    http://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Va ...

  7. .NETFramework类库

    .NET Framework 包括可加快和优化开发过程并提供对系统功能的访问的类.接口和值类型. 为了便于语言之间进行交互操作,大多数 .NET Framework 类型都符合 CLS,因而可在编译器 ...

  8. Sublime Text 添加到右键菜单 带菜单图标

    1.打开 regedit 2.找到节点 HKEY_CLASSSES_ROOT -> * -> Shell 3.右键选择新建“ 项 ” 这个项的名字将作为右键菜单的菜单名称,我用的“ Sub ...

  9. PHP学习笔记五【方法】

    <?php $num1=34; $num2=90; $oper="+"; $res=0; switch($oper) { case "+": $res=$ ...

  10. GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析

    严格的说,DataBinding,DataBound并不是GridView特有的事件,其他的控件诸如ListBox等也有DataBinding,DataBound事件. DataBinding事件MS ...