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的更多相关文章

  1. LeetCode题解-----First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  3. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  4. leetcode 41 First Missing Positive ---java

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  5. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  6. 【leetcode】First Missing Positive

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  7. 【leetcode】First Missing Positive(hard) ☆

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  9. Java for LeetCode 041 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...

随机推荐

  1. Jmeter(三十一)Jmeter Question 之 乱码解读

    众所周知,编码的问题影响着众多开发者,当然见多不怪. 先扒了一个编码的原因,也就是为什么要编码: 计算机中存储信息的最小单元是一个字节即 8 个 bit,所以能表示的字符范围是 0~255 个 人类要 ...

  2. 您真的会修改Active Directory域控制器计算机名称吗

    从我开始做微软这行开始,就经常听说某某公司由于什么原因需要修改Active Directory域控制器计算机名称,但发现好多公司都是直接修改,导致了各种奇葩的问题,今天就给大家推荐一个修改Active ...

  3. 06-001 DependencyInjection 之 LifecycleKind

    IApplicationBuilder 里有如下一成员: IServiceProvider ApplicationServices { get; set; } HttpContext 里有如下两个成员 ...

  4. SAS FORMAT 逻辑库存储 【输出格式 没有找到或无法加载】解决方法

    SAS FORMAT 逻辑库存储 [输出格式  没有找到或无法加载]解决方法:需要指定FORMAT 搜索的路径:OPTIONS FMTSEARCH=(F WORK); 以下为完整示例代码: 00@DA ...

  5. sas share 备忘录

    options comamid=tcp;libname payable 'E:\shouen';proc server authenticate=optional id=share1 msgnumbe ...

  6. boost 学习笔记 2: timer

    boost 学习笔记 2: timer copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html 1:ti ...

  7. 基于Linux命令行KVM虚拟机的安装配置与基本使用

    背景 由于生产环境的服务器并不会安装桌面环境,简单操作的图形化安装也不适合批量部署安装.因此,我还是更倾向于在命令下安装配置KVM虚拟机.结合了一些资料和个人使用的状况,我大致列出了一些基本和常用的使 ...

  8. MySQL表中的数据类型

    数据类型:在表中数据类型主要是限制字段必须以什么样的数据类型传值. 一 整型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT总共有五种,name我们一般用到的也就 ...

  9. MySQL/InnoDB中,对于锁的认识

    MySQL/InnoDB的加锁,一直是一个面试中常问的话题.例如,数据库如果有高并发请求,如何保证数据完整性?产生死锁问题如何排查并解决?我在工作过程中,也会经常用到,乐观锁,排它锁,等.于是今天就对 ...

  10. 文件替换(交互式)Replace

    在增量发版博文里,自动识别目标文件路径是采用了标记目录特征.当部署环境多变时,会多有不便.这个替换程序,在使用时需要手动输入目标目录.当需要进一步自动化时,可以与 fint 配合使用. 程序发布时,需 ...