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 ...
随机推荐
- WWDC 2017, 让我们看看 iTunesConnect 有了哪些不同
距离 WWDC 2017 过去已经有 7 天了,小伙伴们是不是已经发现我们的苹果后台和之前的界面有些略微的不同,如果有心的朋友下了 iOS 11 beta 版就会发现设备上的 App Store 界面 ...
- JAVA并发全景图1.1版本
感谢微信群"Spring Boot那些事"兄弟们的热心整理和总结
- vim自动打开跳到上次的光标位置
只需要vimrc里面加一个稍微复杂一点的autocmd就搞定了: if has("autocmd") au BufReadPost * && line(" ...
- [转载] PHP升级导致系统负载过高问题分析
原文:http://chuansongme.com/n/797172 背景 据XX部门兄弟反应, 其在将PHP从5.3.8 升级到5.5.13 时, 开始运行正常, 运行一段时间后, 系统负载变高,达 ...
- 深入分析AIDL原理
深入分析AIDL原理 分类: Android2011-11-18 17:29 6522人阅读 评论(1) 收藏 举报 descriptorcallbackservicenullinterfaceser ...
- bzoj 3192 删除物品
Written with StackEdit. Description 箱子再分配问题需要解决如下问题: (1)一共有\(N\)个物品,堆成\(M\)堆. (2)所有物品都是一样的,但是它们有不同的优 ...
- asp.net core microservices 架构之Task 事务一致性 事件源 详解
一 aspnetcore之task的任务状态-CancellationToken 我有一篇文章讲解了asp.net的线程方面的知识.我们知道.net的针对于多线程的一个亮点就是Task,net clr ...
- 重装Oracle时出现SID已存在问题的解决办法
重装Oracle时出现SID已存在问题的解决办法 手机打开 注意安装oracle服务器的环境,不稳定导致数据库出现问题,后果很严重! 方法如下: 1.开始->设置->控制面板-&g ...
- Spring IOC容器的初始化—(一)Resource定位
前言 上一篇博文“ Spring IOC是怎样启动的 ”中提到了refresh()方法,这个就是容器初始化的入口.容器初始化共有三个阶段: 第一阶段:Resource定位 第二阶段:BeanDefin ...
- devops 几个方便的工具
1. fake API [canned](https://github.com/sideshowcoder/canned ) fake API. [wiremock](ht ...