[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 ...
随机推荐
- 《how to design programs》12章函数复合
我们写代码时要学会适应辅助函数.作者提出了一个问题,如何对一个表排序.排序函数读取一个表,产生另一个表.排序函数的合约和用途如下: (sort empty) ;; expected value: em ...
- MFC修改任务栏图标及程序运行exe图标
修改左上角的图标和任务栏里图标 在对话框构造函数中 1 CTestDlg::CTestDlg(CWnd* pParent )2 : CDialog(CTestDlg::IDD, pParent)3 { ...
- C# Switch is Type
常规用法: Type t = sender.GetType(); if (t == typeof(Button)) { var realObj = (Button)sender; // Do Some ...
- Clone使用方法详解【转载】
博客引用地址:Clone使用方法详解 Clone使用方法详解 java“指针” Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...
- MongoDB C Driver and APIinstances linux MongoDB安装配置
<一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!
- U-Boot 启动过程和源码分析(第二阶段)-main_loop分析
1> main_loop common/main.c /******************************************************************** ...
- Oracle学习笔记(2)——过程和函数
过程和函数统称为PL/SQL子程序,通过输入.输出参数或输入/输出参数与其调用者交换信息.他们是被命名的PL/SQL块,被编译后存储在数据库中,以备执行.因此,可以在数据库中直接按名称使用它们. 1. ...
- vim中对文本的选择
本文主要解说vim中对文本的选择,vim中选择文本分为: (1)选择字符 ---- 命令行模式下输入小写v (2)选择行 ---- 命令行模式下输入大写V (3)选择块 ---- ...
- swift通过摄像头读取每一帧的图片,并且做识别做人脸识别
最近帮别人做一个项目,主要是使用摄像头做人脸识别 github地址:https://github.com/qugang/AVCaptureVideoTemplate 要使用IOS的摄像头,需要使用AV ...
- yum安装epel库后,安装软件总是提示Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again(无法检索epel仓库)
解决方法: vi /etc/yum.repos.d/epel.repo 将baseurl中的注释取消,将mirrorlist该列注释掉即可. 附图如下: