[Leetcode][Python]41: First Missing Positive
# -*- 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的更多相关文章
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
随机推荐
- Asp.net web服务处理程序(第六篇)
四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...
- [转]Activemq管理和基本介绍
1.ActiveMQ服务器工作模型 通过ActiveMQ消息服务交换消息.消息生产者将消息发送至消息服务,消息消费者则从消息服务接收这些消息.这些消息传送操作是使用一组实现 ActiveM ...
- 行变列 pivot
SELECT p.City , [临时] , [会员] , VIPFROM ( SELECT c.CustomerID , c.City , CASE WHEN COUNT(o.OrderID) &l ...
- Java对象在JVM中的生命周期
当你通过new语句创建一个java对象时,JVM就会为这个对象分配一块内存空间,只要这个对象被引用变量引用了,那么这个对象就会一直驻留在内存中,否则,它就会结束生命周期,JVM会在合适的时 ...
- poj 3308 (最大流)
题意:n*m的地图,给出L个火星人登陆的坐标,要在火星人登陆地球的瞬间全部消灭他们,有一种激光枪,一次可以消灭一行(或一列),消灭一行(或一列)有不同的代价,总代价是所有激光枪的代价之积. 思路:之前 ...
- C语言头文件组织
一.全局变量单独编写(很值得借鉴). 一般习惯将不同功能模块放到一个头文件和一个C文件中. 例如是写一些数学计算函数: //mymath.h #ifndef _mymath_H #define _my ...
- hdu 4611
2013hdu多校联赛二的第一题,当时队友说两个盒子个数的最小公倍数是周期, 如果两个数的最小公倍数比较大的时候(最大是9999900000),如果遍历求的话肯定会超时 当时想找各种规律,都没找到,最 ...
- linux下tar.xz 文件解压
在linux下下载源码文件安装时有些会遇到tar.xz文件的解压,习惯了tar解压缩,第一次遇到.xz文件还是有点迷惑,google 如下,解压这种格式的文件需要xz工具,如果xz工具没有安装,则安装 ...
- LINQ To XML的一些方法
1.Elements() 返回XML文档或片段的所有第一级元素. 2.Descendants() 返回XML文档或片段中的所有子元素(所有级别的子元素). 3.Attributes() 返回当前选中元 ...
- 如何让你的SQL运行得更快
人们在使用SQL时往往会陷入一个误区,即太关注于所得的结果是否正确,而忽略了不同的实现方法之间可能存在的性能差异,这种性能差异在大型的或是复杂的数据库环境中(如联机事务处理OLTP或决策支持系统DSS ...