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. Zabbix 更改监控项的应用级

  2. String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法

    今天在用@RequestParam(required=false) int XXX 取参数的时候,当参数没有的时候Spring默认赋值为空.而此时使用基本类型int,所以报错,建议使用包装类 Inte ...

  3. [UE4]需要保存的数据

    数据存储原则是“相关的放在一起,不相关的分开”. 如果存档有几十上百兆,如果放在一起存储容易出现问题(特别是网络游戏):断线.电脑死机.游戏出错等等,存档就会损坏,所以一定要分割存储.

  4. Linux镜像源

    1. 国内镜像源 (1.)备份原有镜像源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...

  5. C#一年中有多少周方法和js一年中第几周

    最近在做一个时间插件,用的是jquery-daterangepicker ,现在分享一下查询时间是一年中的第几周的js方法  和 一年中有多少周的C#后台方法,默认是按照周一为一周的开始,如果一年的第 ...

  6. MySQL 删除重复数据实例

    如何删除重复数据 业务场景:删除评论表中对同一订单同一商品的重复评论,只保留最早的一条. 查看是否存在对于同一订单同一商品的重复评论. SELECT order_id,product_id,COUNT ...

  7. MySql 索引优化实例

    查询语句 SELECT customer_id,title,content FROM `product_comment` WHERE audit_status=1 AND product_id=199 ...

  8. FileMaker Server 15 Windows 安装

    FileMaker 国外流行,国内研究的人不多.这玩意的好处是: 标准的数据库软件,同时带界面,相当于Access. 手机上安装FileMaker Go,Windows & IOS上安装 Fi ...

  9. [SQL]用SQL语句断开某个数据库的所有活动连接

    USE master go IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[P_KillConnectio ...

  10. POI操作Excel(二)

    注意:HSSFWorkBook对应2003版Excel      XSSFWorkBook对应2007以上的Excel 一.创建时间单元格 public void helloPoi3() throws ...