题目来源

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. BZOJ3356 : [Usaco2004 Jan]禁闭围栏

    首先将坐标离散化,考虑从左往右扫描线 碰到插入操作则插入 碰到删除操作的: 当前包含i的矩形数=y1在[1,y2[i]]之间的矩形数-y2在[1,y1[i]-1]之间的矩形数 用两棵树状数组维护即可, ...

  2. Centos6.4 yum安装MariaDB5.5

    vi /etc/yum.repos.d/MariaDB.repo 加入下面内容 [mariabd]name=MariaDBbaseurl=http://yum.mariadb.org/5.5.34/c ...

  3. POJ 1191 棋盘分割(DP)

    题目链接 大体思路看,黑书...其他就是注意搞一个in数组,这样记忆化搜索,貌似比较快. #include <cstdio> #include <cstring> #inclu ...

  4. ubuntu 14.04 https 形式安装docker 私有库 harbor

    起始目录/root,root 登陆后,直接在该目录进行下面的命令 下载harbor 预编译包 0.4.5 准备通过域名 reg.server.com 来访问镜像库所以需要在/etc/hosts 文件中 ...

  5. Web前端开发规范文档你需要知道的事--HTML、css、js、文档等需要规范内容

          规范目的 为提高团队协作效率,便于后台人员添加功能及前端后期优化维护,输出高质量的文档,特制订此文档.本规范文档一经确认,前端开发人员必须按本文档规范进行前台页面开发.本文档如有不对或者不 ...

  6. 相关css 细节处理 neat.css

    人性化的细节处理 例如: textarea 默认只能垂直拖动,防止宽度改变破坏布局. textarea { resize: vertical; } 汉字字号小于 12px 不易阅读,为 <sma ...

  7. FZU 2092 bfs+记忆化搜索

    晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...

  8. hdoj-1233-还是畅通工程

    题目:hdoj-1233 题解: 本题是典型的最小生成树问题,给出的是无向图,这里使用的方法是Prim最小生成树算法. Reference Prim算法参照:最小生成树-Prim算法和Kruskal算 ...

  9. java build path->source folder分析

    1.build path下的source folde,指的是项目存放源码的位置,即存放Java代码的位置!!! 如果将一个文件夹设为java build path里的source folder下,则这 ...

  10. PIVOT 用于将列值旋转为列名

    PIVOT 用于将列值旋转为列名(即行转列),在 SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT 的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )A ...