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 ...
随机推荐
- Electron 使用 Webpack2 打包应用程序
Electron 使用 Webpack2 打包应用程序 前两天看了一下使用 Electron 来开发应用程序,今天说说所怎样集成 Electron 和 Webpack2 来打包应用程序. 安装依赖库 ...
- [置顶]
Android Shape一些新玩法?
敏少咨讯: 1.生活琐事篇 最近由于公司赶项目所以偷懒了,博客没有及时更新,还请小伙伴们手下留情啊!最近发生了很多趣事,就在今天我们学校退书籍费,这可把我开心的哈哈!无缘无故又有钱了,嘿嘿,刚好五一出 ...
- poj2378(dfs,树形dp)
和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...
- Java发展前景与职业方向解析
大多数人选择Java可能只是因为听说Java前景好.Java比较好找工作.Java语言在TIOBE排行榜上一直位于前三等等之类的原因,但是Java具体好在哪里,心里却是没有什么概念的.本文为你解答学J ...
- 特例模式(Special Case Pattern)与空对象模式(Null Pointer Pattern)—— 返回特例对象而非 null
返回 null 值,基本上是在给自己增加工作量,也是给调用者添乱.只有一处没有检查返回的是否为 null,程序就会抛 NullPointerException 异常. 如果你打算在方法中返回 null ...
- git clone all branch and create a empty branch
/******************************************************************** * git clone all branch and cre ...
- HDU - 6197:array array array (简单LIS)
One day, Kaitou Kiddo had stolen a priceless diamond ring. But detective Conan blocked Kiddo's path ...
- Switch能否使用String做参数
在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted 可以自动转换为整 ...
- JsQuick--个人封装的Js库
JsQuick 该库为本人封装的Js库,尚未进行浏览器兼容 /** * 快速框架 版本:1.0.0 * 日期:2015.02.26 * 作者:简楚恩 */ /** * 快速获取控件类 */ var $ ...
- php-fpm.conf 配置文件详解
php-fpm.conf 配置文件详解 [global] pid = run/php-fpm.pid error_log = log/php-fpm.log log_level = notice # ...