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. T-SQL 事务

    use StudentManager go declare @errorSum int --定义变量,用于累计事务执行过程中的错误 --初始化为0,即无错误 begin transaction beg ...

  2. Centos7 配置ssh连接

    Centos7 配置ssh连接 1.检查是否安装openssh-server:#yum list installed | grep openssh-server 安装openssh-server:#y ...

  3. MFC+mongodb+nodejs 数据库的读取与写入操作

    首先通过nodejs和mongodb建立后端服务器 一.在windows平台下启动mongodb服务器 1.进入mongodb的安装目录,并进去bin目录启动mongod 2.在d盘建立mongodb ...

  4. 第11章 拾遗5:IPv6和IPv4共存技术(1)_双栈技术和6to4隧道技术

    6. IPv6和IPv4共存技术 6.1 双栈技术 (1)双协议主机的协议结构 (2)双协议栈示意图 ①双协议主机在通信时首先通过支持双协议的DNS服务器查询与目的主机名对应的IP地址. ②再根据指定 ...

  5. delphi中Application.MessageBox函数用法详解

    delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...

  6. mybatis的插件,挺好支持下

    利用 Mybatis-generator自动生成代码http://www.cnblogs.com/yjmyzz/p/4210554.html Mybatis 通用 Mapper3 https://gi ...

  7. form表单钩子,局部钩子和全局钩子

    form表单源码解析: 局部钩子: 全局钩子:

  8. 【CF1132F】Clear the String (DP)

    /* 区间dp题目, 考虑当前区间l,r 是可以枚举最后一次拿的分界点来考虑最右边节点是不是具有贡献 */ #include<cstdio> #include<algorithm&g ...

  9. flask 简单的语音识别

    from aip import AipSpeech,AipNlp #AipNlp 为自然语言处理 """ 你的 APPID AK SK """ ...

  10. linux文件基本权限-基本权限的修改

    基本权限的修改 当我们在linux或unix系统的terminal输入"ls -l"命令时,将输出文件的详细信息.第一列,如“drwxr-xr-x”就是文件的权限信息. yinti ...