题目来源

https://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.


题意分析


Input:a list with many numbers

Output:the first missing positive number

Conditions:最小的missing 正整数,注意要0(n)级别的算法


题目思路


利用字典去做,因为字典查询是0(n)的,不过会使用0(n)的空间,AC了,但是网上说要用triky,可能是leecode评测放宽了……以后再回来看看


AC代码(Python)


 _author_ = "YE"
# -*- coding:utf-8 -*- class Solution(object):
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
len1 = len(nums)
if len1 == 0:
return 1
dic = {}
for num in nums:
if num > 0:
dic[num] = num
for i in range(1, len1 + 1):
if dic.get(i, -1) == -1:
return i
return len1 + 1

[LeetCode]题解(python):041-First Missing Positive的更多相关文章

  1. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

  2. Java for LeetCode 041 First Missing Positive

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

  3. LeetCode 041 First Missing Positive

    题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...

  4. LeetCode(41)First Missing Positive

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

  5. leetcode第40题--First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  6. LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  7. 041 First Missing Positive 第一个缺失的正数

    给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...

  8. LeetCode题解-----First Missing Positive

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

  9. LeetCode题解41.First Missing Positive

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

随机推荐

  1. input:focus

    input:focus,select:focus,textarea:focus{outline:-webkit-focus-ring-color auto 5px;outline-offset:-2p ...

  2. codeforces round #201 Div2 A. Difference Row

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  3. 【BZOJ】2463: [中山市选2009]谁能赢呢?(博弈论)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2463 好神的证明! 首先对于n是偶数,一定能被1*2的骨牌覆盖!所以从起点开始,先手一定走的是骨牌的 ...

  4. List 中对象属性排序

    有几个方法可以实现:让 Student 实现Comparable接口,或是实例化一 个比较器, 现在用 Comparator 比较器实例来做一个:ComparableTest.java import  ...

  5. 在Windows2003上安装Active Directory Management Gateway Service

    为了让基于Windows2003的域控能够被Powershell管理,必须安装KB968934,但是直接安装会报以下的错误,必须先安装NDP35SP1-KB969166.但是"因为基于 Wi ...

  6. 去掉网址中的 html编码

    修改 web\urlManager  createUrl函数,去掉 urlEncode函数

  7. 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

    有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的. 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就 ...

  8. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  9. Linux 计划任务 Crontab 笔记与总结(5)crontab 常见错误与案例

    ① 环境变量 cd ~ pwd 就会到你登陆的那个用户的根目录下 ls -a 能够查看到 .bash_profile 这个文件 vim .bash_profile 这里面设置了一些环境变量. 可以设置 ...

  10. selenium 使用笔记

    下面一段代码是使用selenium访问网页一个小实例 #!/usr/bin/python# -*- coding: utf-8 -*- '''Created on Dec 6, 2013 @autho ...