【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 ...
随机推荐
- Ceph rgws客户端验证
修改/etc/ceph/ceph.conf文件,加入rados gw监听的端口 [client.rgw.rgws] rgw_frontends = "civetweb port=80&quo ...
- HDU 4864 Task(贪心)
HDU 4864 Task 题目链接 题意:有一些机器和一些任务.都有时间和等级,机器能做任务的条件为时间等级都大于等于任务.而且一个任务仅仅能被一个机器做.如今求最大能完毕任务.而且保证金钱尽量多 ...
- html调用servlet(JDBC在Servlet中的使用)(1)
1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...
- Objective-C:NSString类的常见用法
几种常见的用法为:字符串的创建.字符串的搜索.字符串的比较.字符串的转换 用途一:字符串的创建 void ex1() { //1.常量字符串的对象 NSString *str1 = @"he ...
- Informatica 常用组件Source Qualifier之七 使用排序端口
使用已排序端口时,PowerCenter 将添加端口至默认查询中的 ORDER BY 子句.PowerCenter Server 将添加配置的端口号,从源限定符转换的顶部开始.在映射中包括以下任何转换 ...
- Java JDBC数据库链接
好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...
- request和request.form和request.querystring的区别
asp中获取传递的参数,一般用request或者用request成员函数request.form,两种方式都可以获取页面表单传递过来的参数值,一直没留意两种方法有什么区别,我一般喜欢用request( ...
- WebSettings 文档 API 翻译 常用设置
. setDefaultFontSize(int size) Sets the default font size. The default is 16. setDefaultTextEncodin ...
- JQuery缓冲加载图片插件lazyload.js的使用方法
lazyload.js是一个基于JQuery的插件,可以用来缓冲加载图片.如果一个网页很长并且有很多图片的话,下载图片就需要很多时间,那么就会影响整个网页的加载速度,而这款延迟加载插件,会通过你的滚动 ...
- 交叉编译git
git依赖openssl.zlib. 首先编译openssl ./Configure linux-armv4 shared 修改Makefile,CC.RANLIB.MAKEDEPPROG为对应的交叉 ...