【LeetCode】41. First Missing Positive (3 solutions)
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.
解法一:O(nlogn) time and O(1) space
无脑解法-->先排序O(nlogn)
思想如下:
1、略去非正的前缀数。
2、记下一个待匹配的正整数为tag。
考虑重复,如果A[i]等于tag,则tag++
如果A[i]大于tag,则返回tag
A[i]不可能小于tag,由排序可以保证。
class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
sort(A,A+n);
int i = ;
while(i < n && A[i] <= )
i ++;
if(i == n)
return ;
int tag = ;
for(; i < n; i ++)
{
if(A[i] > tag)
//miss the tag
return tag;
else if(A[i] == tag)
//next positive
tag ++;
else
;
}
//i==n, miss the tag
return tag;
}
};

解法二:O(n) time and O(n) space
稍作思考就可以知道,n容量的数组,最多覆盖的正整数为连续的1~n
也就是说,丢失的正整数要么出现在1~n中,要么就是n+1
因此可以构造大小为n的数组v,v[i]记录i+1这个数字是否出现在A中。
如果tag全true则返回n+1,否则返回第一个tag为负的下标+1
class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
//tag[i] means whether i+1 exists in A
//at most 1~n, then return n+1
vector<bool> tag(n, false);
for(int i = ; i < n; i ++)
{
if(A[i] > && A[i] <= n)
tag[A[i]-] = true;
}
for(int i = ; i < n; i ++)
{
if(tag[i] == false)
return i+;
}
return n+;
}
};

解法三:O(n) time and O(1) space
解法二中我们构建了新的数组tag来记录每个正整数是否出现在A中,
其实可以省略tag数组,直接在A中记录。这点借鉴了yuyibestman想法。
具体做法为,先将A划分为正整数与非负数。这点类似快排的partition。
A[i]的正负号记录i+1这个数字是否出现在A中。
由于只是符号取负,其值可以通过绝对值函数恢复。这样就代替了解法二中的tag数组了。
class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
//if A[i] is negative, i+1 exists in original A
//partition, non-negative only
int low = ;
int high = n-;
int end = n-;
while(low <= high)
{
while(low <= high && A[low] > )
low ++;
//to here,
//case low > high: partition finished, high point to the end of the new array
if(low > high)
{
end = high;
break;
}
//case A[low]<=0: low point to the first non-positive element
while(high >= low && A[high] <= )
high --;
//to here,
//case high < low: partition finished, high point to the end of the new array
if(low > high)
{
end = high;
break;
}
//case A[high]>0: high point to the first positive element
swap(A[low],A[high]);
}
for(int i = ; i <= end; i ++)
{
//check whether num is in A, and set A[num-1] to be negative
int num = abs(A[i]);
if(num <= end+)
{
if(A[num-] > )
A[num-] *= -;
}
//out of range 1~end+1
}
for(int i = ; i <= end; i ++)
{
if(A[i] > )
return i+;
}
return end+;
}
};

【LeetCode】41. First Missing Positive (3 solutions)的更多相关文章
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- leetcode problem 41 -- First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LEETCODE】41、905. Sort Array By Parity
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- Java 动态向 JTable 中添加数据
import java.awt.Toolkit; import javax.swing.SwingUtilities; import javax.swing.UIManager; import jav ...
- HDU1226:超级密码(BFS)
Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进 ...
- Merge Interval leetcode java
题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6] ...
- Java 线程内异常处理
Thread的run方法是不抛出任何检查型异常(checked exception)的,但是它自身却可能因为一个异常而被终止,导致这个线程的终结.最麻烦的是,在线程中抛出的异常即使使用try...ca ...
- mysql数据库查询优化
上两周一直想办法提高查询速度,取得一点效果,解决了部分问题,记下来以便将来自己查看. 由于公司没有专门的DBA,我自己对mysql数据库也不是很熟悉,而且这个JAVA开发的网络审计系统的管理系统,是经 ...
- Spark RDD关联操作小结
前言 Spark的rdd之间的关系需要通过一些特定的操作来实现, 操作比较多也,特别是一堆JOIN也挺容易让人产生混乱的. 因此做了下小结梳理一下. 准备数据 var rdd1 = sc.makeRD ...
- 敏捷开发方法XP的12个最佳实践
极限编程(eXtreme Programming,简称XP)是一种轻量级.高效.低风险.柔性.可预测的.科学的软件开发方法,其特性包含在12个最佳实践中. 1. 计划游戏 ( Planning Ga ...
- Linux服务器权限管理之sudo高级应用
Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的. Linux系统的 ...
- 装上了Fedora19
超期服役的Aspire黑机器在一个下午主动退役了,为了填补它留下的空白,趁JD减价入手了一台宏碁(acer) SQX4610 120N,就为了玩Linux. 这机器用光驱启动有些特殊,需要在启动时不断 ...
- (笔试题)删除K位数字
题目: 现有一个 n 位数,你需要删除其中的 k 位,请问如何删除才能使得剩下的数最大? 比如当数为 2319274, k=1 时,删去 2 变成 319274 后是可能的最大值. 思路: 1.贪心算 ...