【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 ...
随机推荐
- 关于SpringCloud微服务架构概念的一点理解
目前微服务是非常火的架构或者说概念,也是在构建大型互联网项目时采用的架构方式. 1.单体架构单体架构,是指将开发好的项目打成war包,然后发布到tomcat等容器中的应用. 假设你正准备开发一款与Ub ...
- C预编译, 预处理, C/C++头文件, 编译控制,
在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的 ...
- 【BZOJ】【1923】【Sdoi2010】外星千足虫
高斯消元解Xor方程组 ZYF Orz 这题……不作死就不会死T^T,用bitset确实比较快,而且可以从string直接转成bitset(构造函数). 但问题是我把转过来以后的顺序搞反了……原本以为 ...
- Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)
导航抽屉(navigationdrawer)是一个从屏幕左边滑入的面板,用于显示应用的主要导航项目.用户能够通过在屏幕左边缘滑入或者触摸操作栏的应用图标打开导航抽屉. 导航抽屉覆盖在内容之上,但不覆盖 ...
- Python并发编程-Memcached (分布式内存对象缓存系统)
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】
公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...
- Mac OS中Java Servlet与Http通信
Java中一个Servlet其实就是一个类,用来扩展服务器的性能,服务器上驻留着可以通过“请求-响应”编程模型来访问的应用程序.Servlet可以对任何类型的请求产生响应,但通常只用来扩展Web服务器 ...
- 牛气冲天的Iframe应用笔记
纵观时下网站,本来网速就有些慢,可是几乎每页都要放什么Banner,栏目图片,版权等一大堆雷同的东西,当然,出于网站风格统一.广告效应的需要,本无可厚非,可毕竟让用户的钱包为这些“点缀“的东西”日益消 ...
- 【Django】Django如何保证并发操作数据一致性问题
代码示例: 使用 select for update 数据库查询 select ... for update 是数据库层面上专门用来解决并发取数据后再修改的场景的,主流的关系数据库 比如mysql.p ...
- Systemd 三部曲 之 PHP7
安装编译php7时需要的依赖包 : yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-deve ...