题目来源

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. python 代码片段9

    #coding=utf-8 # 字符串指示符号 r表示raw u表示unicode mystring=u'this is unicode!--by' print mystring # 'raw'表示告 ...

  2. ios 适应屏幕

    标准做法:auto layout 其中:也可以用代码判断  1.如果不是hardcode的话,又怎么会不兼容呢..  2.如果你全写320*480也是兼容吗 3.写320嘛的就是hardcode啊.  ...

  3. 决策树ID3算法示例

    决策树代码如下: #include "MyID3.h" using namespace std; void ReadData() //读入数据 { ifstream fin(&qu ...

  4. Nodejs - windows的系统变量(环境变量)

    我的电脑-属性-高级-环境变量-系统变量(s)-Path 将Node.exe所在的路径插入Path的变量值(V)中 如 ;E:\nodejs\ 最终效果 C:\Windows\system32;C:\ ...

  5. 数组机、局域网ip查找

    cmd ipconfig 以太网适配器 VMware Network Adapter VMnet8: IPv4 地址 . . . . . . . . . . . . : 192.168.233.1

  6. SEPM安装完之后的一些细节之处

    1. 若SEPM与GUP为同一台主机,则必须在其上也安装SEP, 否则其他客户端无法更新.   2. 先指定GUP,然后指派策略   3. Latest on Manager可以通过离线jdb文件进行 ...

  7. 单词游戏-基于SQLite+Qt的C++项目

    SQLite文件数据库的操作 Qt插件的安装配置 1.VS2008下安装Qt开发包 解压缩4.7.3.rar到C:\Qt\4.7.3\ Qt for VS addin2.安装并配置Visual Ass ...

  8. IOS开发之开篇--CocoaPods安装

    CocoaPods是什么?当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其 ...

  9. asp.net webform杂记

    context.Response.ContentType = "text/plain";            string encodeFileName = HttpUtilit ...

  10. bootstrap static popover

    jq $('.popover').show().css('position','relative');