[LeetCode]题解(python):041-First Missing Positive
题目来源
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的更多相关文章
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- LeetCode 041 First Missing Positive
题目要求:First Missing Positive Given an unsorted integer array, find the first missing positive integer ...
- LeetCode(41)First Missing Positive
题目 Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- leetcode第40题--First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 041 First Missing Positive 第一个缺失的正数
给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...
- LeetCode题解-----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
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
随机推荐
- extjs grid
Ext.onReady(function() { Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif'; Ext.QuickTips.in ...
- 【BZOJ】1041: [HAOI2008]圆上的整点(几何)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=1041 所谓的神题,我不会,直接题解..看了半天看懂题解了.详见hzwer博客 这题呢,我只能 ...
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...
- [BZOJ 4436][Cerc2015]Kernel Knights
[Cerc2015]Kernel Knights Time Limit: 2 Sec Memory Limit: 512 MBSubmit: 5 Solved: 4[Submit][Status][D ...
- Xcode4 使用技巧
Xcode4 使用技巧 使用 xcode4 也有一段时间了,今天整理了一下 xcode4 的一些使用技巧,在这里分享给大家. 设置作者 这里所指的作者就是每个源文件头部注释中的 "Creat ...
- ThinkPHP框架的部署
1.将ThinkPHP框架的框架文件放到想要放置的地方,与创建的应用文件夹同级 2.vhost文件中设置虚拟目录 3.在hosts文件中配置 4.在应用目录中创建入口文件index.php 5.在入口 ...
- github上有android开源项目
下面是一些比较好的开源项目,总共分为5大类,也许对某一些人有用,有些项目也许将来某一天对自己也有用,所以整理到此,希望对大家有帮助.首先声明非原创,这篇blog的目的是分享给那些可能需要的人. htt ...
- MySQL助理配置
基本配置 你需要经常察看以下3个配置项.不然,可能很快就会出问题. innodb_buffer_pool_size:这是你安装完InnoDB后第一个应该设置的选项.缓冲池是数据和索引缓存的地方:这个值 ...
- 【原】Windows下Nexus搭建Maven私服
一.Maven安装 详见Java开发环境搭建 二.Nexus安装 2.1.下载 地址:http://www.sonatype.org/nexus/go/ 选择OSS(ZIP)版本 2.2.安装 将安装 ...
- ZOJ 2975 思维
题意 给出一个矩形 问在其中存在多少子矩形 其四个角上的字母是一样的 一开始暴力写了一发 先枚举行数 再枚举两个列数 再向下枚举行数 判断能否 没有意外的超时了 后来想了想 当我们已经确定两个列数的时 ...