First Missing Positive

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)且只能使用常数的额外空间。

解题思路:

遍历两遍数组,第一遍的目的是使数组下标为 i 的数值设置为 i+1,如没有 i+1 就是其他值(不在1~n范围的值)。

第二遍遍历的目的是返回第一个不满足下标为 i ,值为 i+1 的地方,return i + 1;

想看图,点我。

代码如下:

 class Solution {
public:
int firstMissingPositive(int A[], int n) {
int i = ;
while(i < n){
if(A[i] != i+ && A[i]> && A[i]<=n && A[A[i]-] != A[i])
swap(A[i],A[A[i]-]);
else
i++;
}
for(i = ; i < n; i++){
if(A[i] != (i+))
return i+;
}
return n+;
}
};

【LeetCode练习题】First Missing Positive的更多相关文章

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

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

  2. 【leetcode】 First Missing Positive

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

  3. leetcode 41 First Missing Positive ---java

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

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

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

  5. 【leetcode】First Missing Positive

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

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

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

  7. LeetCode - 41. First Missing Positive

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

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

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

  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] ...

  10. Java [Leetcode 41]First Missing Positive

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

随机推荐

  1. Unattended Setup Software Components (无人值守安装软件组件)

    原文 http://social.technet.microsoft.com/Forums/windows/en-US/d4ad85b4-8342-4401-83ed-45cefa814ec5/una ...

  2. 2014元旦第1周三新的尝试&爬山丢失望远镜

    2014元旦在早起中开始,起来后看了<逃出你的肖申克>系列文章,没有精度只是选择了里面的关键语句和方法论,没有多少意外的收获.然后看了些java系列文章,关于jvm参数配置.运行监控及性能 ...

  3. 锁sql server锁

    锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...

  4. UESTC_方老师分身 I CDOJ 914

    方老师分身 I Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  5. 正则表达式、find、grep、awk、sed

    1.正则表达式    (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成.   (2)基本元字符集及其含义       ^ :只 ...

  6. hdu 5626 Clarke and points

    Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...

  7. SHDP--Working with HBase(三)之HBase+Phoenix实现分页

    先简单讲讲只用HBase来实现分页的思路: HBase利用scan来扫描表,通过startKey,stopKey来确定扫描范围,在需要进行分页时可以结合HBase提供的PagefFilter过滤扫描的 ...

  8. Conquering Keokradong && Get the Containers(二分)

    Conquering Keokradong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. spring MVC做form提交Neither BindingResult nor plain target object for bean name 'command' available

    这两天在做spring3.0 MVC+hibernate3.2的练习中,做简单的form提交, 一直报java.lang.IllegalStateException: Neither BindingR ...

  10. Chord算法实现具体

    背景 Chord算法是DHT(Distributed Hash Table)的一种经典实现.下面从网上无节操盗了一段介绍性文字: Chord是最简单.最精确的环形P2P模型."Chord&q ...