【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 ...
随机推荐
- 使用sqlcmd执行连接的时候一直报有语法错误
1.今天在使用sqlcmd进行执行连接操作的时候一直报有语法错误找了半天. 命令 sqlcmd -S 服务器名称 -U 帐户 -P 密码 示例 sqlcmd -S "LOCALHOST&qu ...
- @Html.ValidationSummary()的使用
@Html.ValidationSummary()用于返回表单在后台验证的结果. 如, 当后台if (ModelState.IsValid)失败后,错误信息就会显示到 @Html.Validation ...
- Fedora 20忘记root密码
1.忘记root密码的情况下.用sudo账户$sudo su就行了. 2.直接sudo passwd root就重置了roor密码了.
- UIScrollView中添加一个视图,实现让其始终固定在某个位置
ScrollView中添加一个视图,实现让其始终固定在某个位置,如最底部的位置.方法是自定义一个继承UIScrollView,重写它的layoutSubviews方法.代码如下: #import &q ...
- Struts2返回Json数据(使用Struts2插件)
这篇我将介绍如何使用Struts2的struts2-json-plugin.jar插件返回JSON数据. 一.其中主要步骤有: 1.将struts2-json-plugin.jar插件拷贝到项目的&q ...
- Intellij IDEA 2016 mybatis 生成 mapper
转载地址:http://gold.xitu.io/entry/57763ab77db2a2005517ae3f
- 灵光一闪-VS设计界面能访问到private修饰的各种控件
大家都知道,用VS设计界面时,VS默认控件的访问修饰符为private,但是我就很奇怪,private修饰的字段不是只有类内部才能访问吗? 好神奇的VS,这到底是怎么实现的?难道就是类似文本编辑器的作 ...
- Tomcat启动失败的解决方法
在使用Tomcat的时候,经常会遇到启动失败的问题:解决方法:1.检查环境变量的配置,jdk的配置2.检查端口是否被占用. 关于环境变量的配置很容易搜到,如果按照网上的教程配置好了,但是还是启动失败的 ...
- CSS 实现底部固定
在制作页面有这样一种现象:当一个HTML页面中含有较少的内容时,Web页面的“footer”部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,让你的页面看上去很不好看,特别是现在宽屏越来越 ...
- [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array
题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...