leetcode:First Missing Positive分析和实现
题目大意:
传入整数数组nums,求nums中未出现的正整数中的最小值。要求算法在O(n)时间复杂度内实现,并且只能分配常量空间。
分析:
一般碰到这种问题,都先对数组进行排序,再遍历数组就可以找到最小的在nums中没有出现过的正整数。但是由于排序的时间复杂度一般为O(nlog2(n)),因此时间复杂度没有达到要求。
之后再转回排序的方式,有一种排序的方式称为桶排序,只要有足够的空间,就可以在O(n)的时间复杂度内完成排序过程。但事实是只能分配常量空间。
只能分配常量空间还要求时间复杂度为O(n),这时候我们就只能打起传入的参数nums的主意,能不能用nums来存储我们的中间结果呢?
我们要做的就是遍历nums,将每个在nums中出现的正整数t写入到nums[t-1]中。在完成了这个过程后,再遍历数组,直到找到一个下标i,使得nums[i]不等于i+1,显然i+1就是nums缺失的最小正整数。
下面给出伪代码:
for(i = 0; i < nums.length; i++)
num = nums[i]
rightPos = num - 1
while(rightPos >= 0 && rightPos < nums.length && nums[rightPos] != num)
temp = num[rightPos]
num[rightPos] = num
num = temp
rightPos = num - 1
上面就是第一阶段的代码,负责将每个在nums中出现的正整数t写入到nums[t-1]中。而当然对于一些负数和过大的正整数(大于n),由于无处可写就会直接被跳过,而这些整数也必然不会包含我们所要求的结果。
再说明一下为什么这段代码能在O(n)时间复杂度内完成。我们称一个下标i为正确的,当且仅当nums[i] = i + 1,而不正确的下标则称为错误的,显然在上面的代码逻辑中一旦一个下标i是正确的,那么就不会有值写入到nums[i]中(nums[rightPos] != num条件保证),即一个正确的下标不会转变为错误的下标。在不考虑内部while循环占用的时间的情况下,for循环总共的时间复杂度为O(n)毋庸质疑。而每次调用while循环,都会将一个错误值纠正为正确的值,而最多只会存在n个正确值,这就意味着while循环总共只会执行n次,而一次while循环内部的动作所耗费的时间复杂度为O(1),故总的时间复杂度就为"for循环不考虑while的时间复杂度"+"for循环内while的时间复杂度"=O(n)+O(n)=O(n)。
而上面这个过程只额外分配了固定的变量数目,因此空间复杂度为O(1)。由于递归也要占用栈空间,即空间复杂度会增加,但这里用while而非递归,因此空间复杂度不会被破坏。
给出Java的解决代码:
public class Solution {
public int firstMissingPositive(int[] nums) {
if(nums.length == 0)
{
return 1;
}
for(int i = 0, bound = nums.length; i < bound; i++)
{
int num = nums[i];
int rightPos = num - 1;
while(rightPos >= 0 && rightPos < bound && nums[rightPos] != num)
{
int tmp = nums[rightPos];
nums[rightPos] = num;
num = tmp;
rightPos = num - 1;
}
}
for(int i = 0, bound = nums.length; i < bound; i++)
{
if(nums[i] != i + 1)
{
return i + 1;
}
}
return nums.length + 1;
}
}
leetcode:First Missing Positive分析和实现的更多相关文章
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Leetcode First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode: First Missing Positive 解题报告
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- LeetCode – First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- LeetCode OJ-- First Missing Positive
https://oj.leetcode.com/problems/first-missing-positive/ 给一列数,找出缺失的第一个正数.要求时间复杂度 O(n) 第一步遍历一遍,找出最大的数 ...
- leetcode First Missing Positive hashset简单应用
public class Solution { public int firstMissingPositive(int[] A) { HashSet<Integer> hash=new H ...
- leetcode First Missing Positive python
class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[in ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
随机推荐
- Lua学习---编译生成lua和luac
众所周知,Lua是一种强大的脚本语言,并且这种语言是用C语言实现的.为什么要学习这门语言?因为它可以增强我看C语言代码的功底. 我下的Lua版本是Lua5.3,关于Lua5.3的简介如下: http: ...
- canvas 绘制环形进度条
结果: 代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...
- (十)js获取日期
//将日期转换成字符串格式输出 function formatDateToString(){ // 先获取对象日期 var oDate = new Date(); // 从该对象中分别拿出所需要的 年 ...
- [HAL]5.中断里调用HAL_Delay()进入死循环的原因
中断里调用HAL_Delay()进入死循环的原因 摘自:http://blog.csdn.net/alwxkxk/article/details/47204677 CUBE生成的程序中, SysTi ...
- notebook查找文件
- Django json处理
转自:http://www.gowhich.com/blog/423 1, 发往浏览器端 前端:jQuery发送GET请求,并解析json数据. url = "http://example. ...
- LeetCode Max Stack
原题链接在这里:https://leetcode.com/problems/max-stack/description/ 题目: Design a max stack that supports pu ...
- 【java规则引擎】drools6.5.0版本中kmodule.xml解析
kmodule.xml文件存放在src/main/resources/META-INF/文件夹下. <?xml version="1.0" encoding="UT ...
- Anaconda 使用conda常用命令
1.首先在所在系统中安装Anaconda.可以打开命令行输入conda -V检验是否安装以及当前conda的版本. 2.conda常用的命令. 1)conda list 查看安装了哪些包. 2)con ...
- input type="file" accept="image/*"上传文件慢的问题解决办法
相信大家都写过<input type="file" name="file" class="element" accept=" ...