[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. ...
随机推荐
- [快速数论变换 NTT]
先粘一个模板.这是求高精度乘法的 #include <bits/stdc++.h> #define maxn 1010 using namespace std; char s[maxn]; ...
- CKEditor实现图片上传
本人用的CKEditor版本为4.3 CKEditor配置和部署参考CKEditor4.x部署和配置. CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星 ...
- 在iOS开发中使用FMDB
在iOS开发中使用FMDB 前言 SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK 很早就支持了 SQLite,在使用时,只需 ...
- A线段树
线段树专题 顾琪坤 1.简介: 打acm的时候,经常会碰到一类问题,比方给你n个数的序列,然后动态的更改某些数的值,然后又动态地询问某个区间的值的和或者其它乱七八糟的东西,对于单个更改或者询问,也许很 ...
- php任何优化的方式下这样第个列表都是再次查询
我们的代码经理是这样的:计算总行数:select count(*) from tablename where -..查询列表select * from tablename where - limit- ...
- 是否采用Sybase形式的自动字符串转义(用 '' 表示 ')
;; 关于php.ini ;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录中. ; 最新版本的php.ini可以在下面两个位置查看: ; h ...
- Hadoop工程包架构解析
Hadoop源码解析 1 --- Hadoop工程包架构解析 1 Hadoop中各工程包依赖简述 Google的核心竞争技术是它的计算平台.Google的大牛们用了下面5篇文章,介绍了它们的计算 ...
- SDR和DDR1/2/3全系列频率对照表
- Scrum会议5
组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.20 已完成内容: 1.游戏中实现了两个按钮消除的算法. 2.在游戏中加入了音乐. 计划完 ...
- jiffies
linux中的jiffies变量 全局变量jiffies用来记录自系统启动以来产生的节拍的总数.启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值.一秒内时钟中断的次数等于H ...