[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 ...
随机推荐
- setAdapter(adapter)空指针nullPointer 解决办法
setAdapter(adapter)空指针nullPointer 解决办法 (2014-06-13 10:01:23) 转载▼ 标签: 旅游 分类: Android开发 如果setAdapter报空 ...
- Farming
Problem Description You have a big farm, and you want to grow vegetables in it. You're too lazy to s ...
- UESTC_男神的约会 2015 UESTC Training for Dynamic Programming<Problem J>
J - 男神的约会 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- hdu1166 树状数组
不知道为什么用C++输入输出死活不过,换成C的就过了... #include <stdio.h> #include <string.h> //================= ...
- 【转】C++容器类
C++容器类 C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等:后者包括set,map,multiset,multimap等. 若需要存储的元素数在 ...
- hdu 5625 Clarke and chemistry
Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...
- 为啥NSString的属性要用copy而不用retain
之前学习生活中,知道NSString的属性要用copy而不用retain,可是不知道为啥,这两天我研究了一下,然后最终明确了. 详细原因是由于用copy比用retain安全,当是NSString的时候 ...
- RMAN数据库恢复之对数据库进行完全介质恢复
RMAN数据库恢复之对数据库进行完全介质恢复环境:控制文件和参数文件SPFILE及归档文件.重做日志文件都在.其它数据文件丢失.恢复方法:使用之前创建的全库备份进行恢复1.删除数据文件: SQL> ...
- C# Winform中执行post操作并获取返回的XML类型的数据
/// <summary> /// 返回指定日期的订单数据 /// </summary> /// <param name="StartDate"> ...
- Stack的三种含义(转载--阮一峰)
作者: 阮一峰 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词其实有三种含义,适用于不同的场合 ...