【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 ...
随机推荐
- asp.net RadioButton控件基础
RadioButton按钮呢,必须要设置groupname属性的值才能将多个RadioButton按钮设置为单选按钮,当AutoPostBack="true"的时候,在change ...
- Android API Level在11前后及16之后时Notification的不同用法
作为刚入门Android的小白,最近在按照郭大神的<第一行代码>在练习,在用到Notification时遇到了一些问题,网上资料比较零散,我这里做了一个总结分析给各位,若有错误,恳请指正~ ...
- SqlServer之表变量和临时表
表变量: 表变量创建的语法类似于临时表,区别就在于创建的时候,必须要为之命名.表变量是变量的一种, 表变量也分为本地及全局的两种,本地表变量的名称都是以"@"为前缀,只有在本地当前 ...
- javascript学习笔记(2)
<html> <head><title>Throwing die</title><script> var canv_width = ...
- Ant Table组件
http://www.cnblogs.com/hujunzheng/p/5689650.html React中使用Ant Table组件 v一.Ant Design of React http:/ ...
- android中onStartActivityForResult无返回值问题
在activity间跳转传递参数,常见方法是通过onStartActivityForResult来做.不过今天使用 onStartActivityForResult的时候已经在上一个activity调 ...
- SD和SDHC和SDXC卡的区别是什么
SD卡,SDHC卡,SDXC卡区别在规格不一样,SD卡最大支持2GB容量,SDHC 最大支持32GB容量,SDXC 最大支持2TB(2048GB)容量,支持SDXC卡的数码设备是兼容支持SD卡与SDH ...
- 如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true
下面这篇文章是从StackOverflow来的.LZ面试的时候遇到了一道面试题:“如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true”,于是LZ做了下面的这样的程序: bool ...
- Siverlight+WCF+Nhibernate 开发之旅(一)
最近正在开发sl程序,考虑了很久,参考了一些框架,令人头疼的数据访问层最终选择wcf+nhibernate,至于为什么选择wcf和nh,个人参考了其他的框架感觉这两者结合从开发效率和便捷性方面比其他的 ...
- 获取考试成绩的sql语句
as score,t_answer.id,t_answer.exams_name,t_answers.answer_id,t_answers.questions_id,t_answers.questi ...