LeetCode: First Missing Positive 解题报告
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.
SOLUTION 1:
使用类似桶排序的方法:
将值放在它应该在的位置,最后再扫描一次得出哪个位置有缺少值。
引自:
http://m.blog.csdn.net/blog/hellobinfeng/17348055
http://n00tc0d3r.blogspot.com/2013/03/find-first-missing-positive.html
http://www.cnblogs.com/AnnieKim/archive/2013/04/21/3034631.html
A few quick thoughts:
- Sort all numbers and iterate through to find the first missing integer? No, most sorting algorithms take time at least O(nlogn).
- How about linear sorting algorithm? No, bucket sort requires O(n) space.
- Mapping all positive integers to a hash table and iterate from 1 to the length of the array to find out the first missing one? No, hash table requires O(n) space.
Then, how to solve this?
Let's take another look at the problem. It is asking for the first missing POSITIVE integer.
So, given a number in the array,
- if it is non-positive, ignore it;
- if it is positive, say we have A[i] = x, we know it should be in slot A[x-1]! That is to say, we can swap A[x-1] with A[i] so as to place x into the right place.
解1:
public int firstMissingPositive1(int[] A) {
// bug 3: when length is 0, return 1;
if (A == null) {
return 0;
} for (int i = 0; i < A.length; i++) {
// BUG 1: TLE , should judge when A[i] - 1 == i;
while (A[i] - 1 != i && A[i] > 0) {
// bug 2: cant exchange a same node: A[A[i] - 1] != A[i]
if (A[i] - 1 < A.length && A[A[i] - 1] != A[i]) {
swap(A, i, A[i] - 1);
} else {
// when the number is out of range, delete it.
A[i] = 0;
}
}
} for (int i = 0; i < A.length; i++) {
if (A[i] <= 0) {
return i + 1;
}
} return A.length + 1;
} public void swap(int[] A, int l, int r) {
int tmp = A[l];
A[l] = A[r];
A[r] = tmp;
}
简化后,解2:
其实交换的条件就是3个:
1: A[i] is in the range;
2: A[i] > 0.
3: The target is different; (如果不判断这个,会造成死循环,因为你交换过来一个一样的值)
// SOLUTION 2:
public int firstMissingPositive(int[] A) {
// bug 3: when length is 0, return 1;
if (A == null) {
return 0;
} for (int i = 0; i < A.length; i++) {
// 1: A[i] is in the range;
// 2: A[i] > 0.
// 3: The target is different;
while (A[i] <= A.length && A[i] > 0 && A[A[i] - 1] != A[i]) {
swap(A, i, A[i] - 1);
}
} for (int i = 0; i < A.length; i++) {
if (A[i] != i + 1) {
return i + 1;
}
} return A.length + 1;
}
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/FirstMissingPositive.java
LeetCode: First Missing Positive 解题报告的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- springboot项目接入配置中心,实现@ConfigurationProperties的bean属性刷新方案
前言 配置中心,通过key=value的形式存储环境变量.配置中心的属性做了修改,项目中可以通过配置中心的依赖(sdk)立即感知到.需要做的就是如何在属性发生变化时,改变带有@Configuratio ...
- JavaFx 中常见的包和类(javafx笔记 )
JavaFx 中常见的包和类(javafx笔记 ) 更多详细内容请参考<Pro JavaFX 8>. javafx.stage 包包含以下类: Stage 类 Stage 类是任何 J ...
- IdentityServer4-端点
一.发现端点 二.授权端点 三.令牌端点 四.UserInfo端点 五.Introspection端点 六.撤销端点 七.结束会话端点 一.发现端点 发现端点可用于检索有关IdentityServer ...
- bzoj 1095 括号序列求两点距离
大致题意: 给一棵树,每个节点最开始都是黑色,有两种操作,1.询问树中相距最远的一对黑点的距离 2.反转一个节点的颜色 一种做法: 建立出树的括号序列,类似这样: [A[B][C]],所以长度为3*n ...
- git 删除分支 远程 && 本地
//查看远程分支 git branch -a //删除远程分支 git branch -r -d origin/branch-name git push origin :branch-name// 或 ...
- ecshop jquery 冲突
遇到冲突在脚本前面加上这句 $(function() { window.__Object_toJSONString = Object.prototype.toJSONString; delete Ob ...
- A - K进制下的大数
https://vjudge.net/contest/218366#problem/A 中间溢出,注意求模. #include<iostream> #include<cstdio&g ...
- 简单理解Linux的Loopback接口
Linu支持环回接口( Loopback Interface),以允许运行在同一台主机上的客户程序和服务器程序通TCP/IP进行通信. A 类网络127就是为环回接口预留的 .根据惯例,大多数系统把I ...
- 01-复杂度1 最大子列和问题(剑指offer和PAT)
01-复杂度1 最大子列和问题 (20分) 给定KK个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., N ...
- crontab不能执行sudo:抱歉,您必须拥有一个终端来执行 sudo
最近做一个可执行shell调度的需求,要求用户输入shell,然后后台定时调度运行.实现大致为:保存用户的输入,设定时间,crontab定时执行用户的输入.但这里涉及到一个安全问题,如何确定用户的输入 ...