题目:

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.

给定一个没有排好序的数组。找出其中第一个消逝的正整数。例如 [1,2,5,4]那就是缺3.

思路:利用counting sort的想法。遍历一次,如果当前的数超出了 1 到 n-1的范围,跳过。如果是1到n-1但是,A[i]不等于i+1,并且在 A[i] - 1 的位置的数不满足 A[A[i]-1]+1的要求,那就将两个位置的数置换。同时,还要在当前位置判断换过来的数是否满足上述要求。这个哥们的图很好理解。

class Solution {
public:
void swap40(int A[], int i, int j) // 其实std有自带的函数swap(A[i], A[j])
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
return;
}
int firstMissingPositive(int A[], int n)
{
if (n <= 0 )
return 1;
for (int i = 0; i < n; ++i)
{
if (A[i] > 0 && (A[i] - 1 < n) && A[i] != i + 1 && A[i] != A[A[i] - 1])
{swap(A, i, A[i] - 1);i--;} // i--是为了和之后的++i持平,因为交换的条件满足的话,交换过来的数还要再判断
}
for (int i = 0; i < n; ++i)
{
if (A[i] != i + 1)
return i + 1;
}
return n + 1;
}
};

leetcode第40题--First Missing Positive的更多相关文章

  1. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  2. LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  3. LeetCode(41)First Missing Positive

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

  4. LeetCode刷题 fIRST MISSING POSITIVE

    Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, a ...

  5. leetcode第40题:组合总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  6. LeetCode OJ:First Missing Positive (第一个丢失的正数)

    在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...

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

    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. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

随机推荐

  1. Android中部署自己的su

    本人博客原文 首先把你的自己的su的放到Android应用程序project的assets文件夹,为了和系统的su区分,我自己的su文件叫做sur. 另外我这里没有考虑x86架构的cpu的手机. 废话 ...

  2. 采用shell脚本统计代码的行数

    刚毕业那会儿有一次去台湾公司面试,我问多行代码怎么写.我从来没有想过这个问题,粗略计算,.惊叹:大概几十万行不行. 最近整理资料,看着eclipse左边全面上市,我觉得这个东西.代码共同拥有的行倒底总 ...

  3. Objective-C基调(4)Category

    OC它提供了一种不同的方式--Category,可以动态地添加新的行为已经存在的类(方法),这确保了较小的类的原始设计,然后逐渐加入扩展. 正在使用Category扩张的上课时间,你并不需要创建一个子 ...

  4. 4.事务提交过程,交易的基本概念,Oracle交易周期,保存点savepoint,数据库的隔离级别

     事务提交过程 事务 基本概念 概念:一个或者多个DML语言组成 特点:要么都成功.要么都失败 事务的隔离性:多个client同一时候操作数据库的时候.要隔离它们的操作, 否则出现:脏读  不可反 ...

  5. IOS的UITextField,UIButton,UIWebView它描述的一些属性和IOS提示图像资源

    有时UI要开发的资源与实际frame不符.这一次,我们要绘制图片 UIImage* image = [[UIImage imageNamed:@"text_field_bg.png" ...

  6. Android物业动画研究(Property Animation)彻底解决具体解释

     前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...

  7. Repository模式

    Repository模式的两种写法与疑惑 现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Reposit ...

  8. php_windows搭建

    Php文件放到: D:\web\Apache2.2\htdocs 修改 D:\web\PHP\php.ini 拓展 修改 D:\web\Apache2.2\conf\httpd.conf  支持php ...

  9. hibernate 一对多关联关系(具体分析)

    在领域模型中, 类与类之间最普遍的关系就是关联关系. 在 UML 中, 关联是有方向的.  以 Customer 和 Order 为例: 一个用户能发出多个订单, 而一个订单仅仅能属于一个客户. 从 ...

  10. jquery 直接调用 wcf,面向服务的SOA架构 ( 第三天)

    所谓万事 具备,只欠东风了!! 接下来就是 wcf 的调用, 首先 在客户端下,随便 写一个 html页面,然后写入如下方法: <script src="scripts/jquery. ...