【LeetCode题意分析&解答】41. 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.
题意分析:
本题是给一个整数的数组,让你按顺序找出第一个缺失的正整数。也就是说从1开始查找,找到了1再找2,这样一直找到缺失的第一个正整数。比如[1,2,0] return 3,[3,4,-1,1] return 2. 要求时间复杂度O(n) ,空间复杂度为常数。
解答:
我们要注意到这样一个事实,数组的下标是有标记意义的。所以我们可以把数字放到相应的下标下面,这样理想情况下所有的正数都能一一对应到0~N-1的下标中,这里面可以使用交换来实现。这样一次循环之后,正整数i应该交换到了i-1下标对应的元素中。然后在进行一次循环查找第一个不符合的元素输出即可。
AC代码:
class Solution(object):
def firstMissingPositive(self, nums):
i, n = 0, len(nums)
while i < n:
if nums[i] > 0 and nums[i] <= n and nums[i] != nums[nums[i] - 1]:
# swap
temp = nums[i]
nums[i] = nums[nums[i] - 1]
nums[temp - 1] = temp
else:
i += 1
for i, v in enumerate(nums):
if v != i + 1:
return i + 1
return n + 1
【LeetCode题意分析&解答】41. First Missing Positive的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- LocalStorage存储
1.localStorage存储服务: .factory('storageSvc', [function () { return { //保存数据 save: function (key, valu ...
- 使用prototype扩展的JavaScript常用函数库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- c++中的const参数,const变量,const指针,const对象,以及const成员函数
const 是constant 的缩写,“恒定不变”的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:“Use const whe ...
- U盘安装win7+CentOS7双系统
决定要好好学习一下Linux了,不管是为了以后技术发展的需要抑或是满足自己的兴趣,都是时候来涉足一下了.我准备在我的ThinkPad X200i(一个老掉牙的老TP本子)上装一个Linux发行版,这里 ...
- CEvent,CSemaphore,CCriticalSection,CMutex
一.用CEvent实现线程同步 事件对象(Event)是最简单的同步对象,它包括有信号和无信号两种状态.在线程访问某一资源之前,也许需要等待某一事件的发生,这时用事件对象最合适.例如,只有在通信端口缓 ...
- Mongodb基础知识----Mongodb权威指南阅读
文档是Mongodb中数据的基本单元,类型关系型数据库中的行,每个文档都有一个键值唯一的键_id.集合可以看做拥有动态模式的表. Mongodb一个实例可以拥有多个相互独立的数据库. Mongodb区 ...
- MySQL管理一些基础SQL语句
.进入information_schema 数据库(存放了其他的数据库的信息) use information_schema; .查询所有数据的大小: ),),'MB') as data from i ...
- Flink资料(7) -- 背压监控
背压(backpressure)监控 本文翻译自Back Pressure Monitoring --------------------------------------------------- ...
- binwalk windows安装和使用方法
binwalk是一个文件的分析工具,旨在协助研究人员对文件进行分析,提取及逆向工程.简单易用,完全自动化脚本,并通过自定义签名,提取规则和插件模块,还重要一点的是可以轻松地扩展. 以上是binwalk ...
- SQL Server 函数执行
在SQL Server 不只是procedure 可以用execute 来执行 function 也是可以的 例子: create function ufn_A( @i as int) returns ...