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. 客户端负载均衡Feign之三:Feign补充

    在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Ap ...

  2. ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等

    ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等 1.返回顶部 1. Java 实例 - 判断数组是否相等  Java 实例 以下实例演示了如 ...

  3. [C#]App.Config

    <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm. ...

  4. tesseract-ocr4.0 安装部署及训练验证码识别

    1.  下载最新版本的leptonica,  leptonica-1.74.1.tar.gz   2. 编译安装 tar -zxvf leptonica-.tar.gz cd leptonica- . ...

  5. Java中常见流的分类及简单讲解

    流在Java中是指计算中流动的缓冲区. 从外部设备流向中央处理器的数据流成为“输入流”,反之成为“输出流”. 字符流和字节流的主要区别: 1.字节流读取的时候,读到一个字节就返回一个字节:字符流使用了 ...

  6. ACCESS常用数字类型的说明和取值范围

    下面是ACCESS常用数字类型的说明和取值范围列表明供参考 数字类型                 范围 Byte(字节)            介于 0 到 255 之间的整型数. Integer ...

  7. MySQL 插件CONNECTION_CONTROL和CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS

    mysql> show variables like 'plugin_dir';+---------------+------------------------------+| Variabl ...

  8. C#列的一些操作

    //变更列名 DataTableA.Columns["原来的列名"].ColumnName="新的列名";

  9. c#数组去重

    第一种: string[] stringArray = { "aaa", "bbb", "aaa", "ccc", &q ...

  10. flume用场景及架构原理

    Flume是什么 1.flume可以将采集到的数据存储到HDFS上,也可以放在Hbase上. 2.flume就是一个中间插件,他的作用就是屏蔽数据源和数据存储系统的差异.可以在不同的数据源采集数据,因 ...