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 ...
随机推荐
- 在ubuntu14.4里编译UBOOT出错
出错信息如下: OBJCOPY examples/standalone/hello_world.bin LDS u-boot.lds LD u-boot./scripts/dtc ...
- Android 进阶10:进程通信之 Messenger 使用与解析
读完本文你将了解: Messenger 简介 Messenger 的使用 服务端 客户端 运行效果 使用小结 总结 代码地址 Thanks 前面我们介绍了 AIDL 的使用与原理,这篇文章来介绍下 A ...
- go set up on ubuntu
sudo apt-get install golang-go package main import ( "fmt" "runtime" ) func main ...
- vue前端开发那些事——前言
如上图所示,用vue开发一个小型网站所涉及到的知识点.这只是前端部分已经这么多了.接下来我分解开来说. 1.Node 当我们开发vue项目的时候,首先要安装Node.js,那么我们即使当时不理解为什么 ...
- Java中小数保留问题
方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 ...
- 笔记:Why don't you pull up a chair and give this lifestyle a try?
Why don't you pull up a chair and give this lifestyle a try? Why don't you pull up a chair and give ...
- 第21篇 ubuntu安装ftp服务器(转载)
ubuntu安装ftp服务器 1: 安装vsftpd ~$ sudo apt-get install vsftpd ubuntu10.10自己装了,这步省略. 2: 配置vsftpd 2.1 修改vs ...
- VS2010环境下MFC使用DataGrid绑定数据源
如果MFC的软件中 使用DataGrid控件后,在别的电脑上不能运行行,需要拷贝一个 MSDATGRD.ocx 和msstdfmt.dll 文件在软件的目录中,并写一个批处理文件 reg.dat 文 ...
- Ubuntu14.04安装搜狗输入法的一点小问题
难得搜狗输入法支持ubuntu,果断下载尝试一把. 官网:http://pinyin.sogou.com/linux/ 官网教程:http://pinyin.sogou.com/linux/help. ...
- VS2017自定义代码片段, 实现快捷输入
点击VS2017的工具→代码片段管理器, 下图: 语言选择C#, 路径定位到 Visual C#, 然后复制这个路径在电脑中打开 这里以增加 crk 快捷方式输出 Console.ReadKey()来 ...