【LeetCode练习题】First Missing Positive
First Missing Positive
Given an unsorted integer array, find the first missing positive integer.
For example,
Given[1,2,0]return3,
and[3,4,-1,1]return2.Your algorithm should run in O(n) time and uses constant space.
题目意思:
返回第一个缺失的正数。
时间复杂度应该在O(n)且只能使用常数的额外空间。
解题思路:
遍历两遍数组,第一遍的目的是使数组下标为 i 的数值设置为 i+1,如没有 i+1 就是其他值(不在1~n范围的值)。
第二遍遍历的目的是返回第一个不满足下标为 i ,值为 i+1 的地方,return i + 1;
代码如下:
class Solution {
public:
int firstMissingPositive(int A[], int n) {
int i = ;
while(i < n){
if(A[i] != i+ && A[i]> && A[i]<=n && A[A[i]-] != A[i])
swap(A[i],A[A[i]-]);
else
i++;
}
for(i = ; i < n; i++){
if(A[i] != (i+))
return i+;
}
return n+;
}
};
【LeetCode练习题】First Missing Positive的更多相关文章
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
随机推荐
- Jedis超时时间设置梳理
JedisConnectionException: Unexpected end of stream #932 Repeatable exception and for the life of me, ...
- 开源欣赏wordpress之intall.php
引导式安装 $weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) ...
- Linux系统编程(13)——Shell的基本语法
按照惯例,Shell变量由全大写字母加下划线组成,有两种类型的Shell变量:环境变量和本地变量. 环境变量: 环境变量可以从父进程传给子进程,因此Shell进程的环境变量可以从当前Shell进程传给 ...
- archlinux相关资料整理
Arch linux Arch Linux Wiki Arch linux Wiki Markdown Arch Wiki python continuing ...
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
B - 邱老师降临小行星 Time Limit: 10000/5000MS (Java/Others) Memory Limit: 65536/65535KB (Java/Others) Su ...
- apache FtpServer 整合spring部署
我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...
- 使用Memcache在PHP中调试方法的介绍及应用
使用Memcache在PHP中调试方法的介绍及应用 如果我们在网络开发中,特别是大访问量的web项目开发中,为了提高响应速度,减少数据查询运算,那么我们都会选用memcahce.首先我们必须要安装,接 ...
- ZOJ Monthly, June 2014 月赛BCDEFGH题题解
比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...
- linux C读取数据库
上次我们已经共同学习了在Linux下C连接数据库,下面一起学习用C语言来操作数据库. 1,首先要打开mysql的服务 [root@bogon ~]# service mysqld statusmysq ...
- 用maven骨架生成项目速度慢的问题
最近从IntelliJ Idea 14的Community版本切换到Ultimate. 问题出现 最近从IntelliJ Idea 14的Community版本切换到Ultimate,key是从网络上 ...