Leetcode 题解 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(n)时间,而且是o(1)空间。
一开始想的是记录正数的个数和他们的和,利用1~n的公式求和,减去sum,看缺了哪个。
指导提交错误,发现居然可以输入 [1,1]。原来还可以有重复的。
想到应该是利用了原来的数组。
用原来的数组做哈希。
对于每一个数,把它放到它应该在的位置上。比如一个4,把它放到a[3]的位置上。
原来的数怎么办呢?调换。a[3]原来的数就放在a[i]的位置。反复调换,反复调换。当然其中有坑的,我提交了三次才AC了。
下面代码的执行效果就是:
比如输入 -3 1 5 3 2
index 0 1 2 3 4
————————————————
i = 0: -3 1 5 3 2 负值跳过了
i = 1: 1 -3 5 3 2 swap(-3,1)
i = 1: 1 -3 5 3 2 负值跳过
i = 2: 1 -3 3 swap(5,2)
i = 2: 1 2 -3 3 5 swap(2,-3)
i = 2: 1 2 -3 3 5 负数跳过
i = 3: 1 2 3 -3 5 swap(-3,3)
i = 4: 1 2 3 -3 5 负数跳过
最后变成了 1 2 3 -3 5
很明显,缺的是4啦。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int i,j,n,tmp;
vector<int> &a = nums;
n = a.size();
if(n == ) return ;
for(i = ,j = ; i < n; i++)
{
if(a[i] > && a[i] != i + ) //一个正数不在它自己的位置上,
{
tmp = a[i] - ; //tmp是它应该呆的位置
if(tmp < n && a[tmp]!= a[i]) //防止下标越界和 死循环
{
swap(a[tmp],a[i]);
i--;
} //这个调换最多会有多少次呢? 最多也就是n次。因为n次以后,n个数都会在正确的位置了。
}
}
for(i = ; i<n; i++) //数一数,看看中间却了哪个呢?如果都不缺,那就是1..n的连续数组,就返回 n + 1
{
if(a[i] != i + )
break;
}
return i + ;
}
};
Leetcode 题解 First Missing Positive的更多相关文章
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [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 ---------------------------------------------------------- ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
随机推荐
- [转][Centos]一、了解关机
来自:https://blog.csdn.net/ronmy/article/details/79117390 Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1 ...
- Linux下使用curl查看http请求各阶段耗时
1. 准备文件模版(curl.txt) \n time_namelookup: %{time_namelookup}\n time_connect: %{time_connect}\n time_ap ...
- Laravel 5.5 FormRequest 自定义错误消息 postman调试时X-Requested-With设为XMLHttpRequest
Laravel 5.5 FormRequest 自定义错误消息 使用FormRequest进行表单验证,就不用让验证逻辑和控制器里面的逻辑都混在一起.但在使用的时候呢,发现json错误返回的数据,与我 ...
- Java常用的线程池
Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...
- Maven Project pom.xml属性解析
pom.xml文件: groupId 定义了项目属于哪个组,根据自己的情况命名,比如谷歌公司的angular项目,就取名为 com.google.angular artifactId 定义了当前Ma ...
- ubuntu14.04安装telnet
1.首先查看telnet运行状态 netstat -a | grep telnet 输出为空,表示没有开启该服务 2.安装openbsd-inetd apt-get install openbsd-i ...
- 通过OTA的方式在局域网分发iOS应用
公司的一个项目有Android和iOS的app,Android的下载和安装都很方便,不过iOS有些麻烦,因为项目本身有些限制,主要有以下一些障碍:1.iOS的版本不是通过Appstore分发.2.出于 ...
- TFTP(Trivial File Transfer Protocol,简单文件传输协议)
网络特性 通常使用UDP 69端口(据说可改成TCP) 与FTP区别 轻量级,适用于传输小文件,当然功能也少些,比如没有列出目录功能,不进行认证
- (转)深入sql server中的事务
原文地址:http://www.cnblogs.com/chnking/archive/2007/05/27/761209.html 参考文章:http://www.cnblogs.com/zhuif ...
- mac gcc develop
1:check version gcc -v / g++ -v 2:compile gcc *.c / g++ *.cpp outfile: a.out 3:excute ./a.out ...