题目:

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. Team Foundation Server 2015使用教程--团队项目创建

  2. SQL开发中容易忽视的一些小地方(四)

    原文:SQL开发中容易忽视的一些小地方(四) 本篇我想针对网上一些对于非聚集索引使用场合的某些说法进行一些更正. 下面引用下MSDN对于非聚集索引结构的描述. 非聚集索引结构: 1:非聚集索引与聚集索 ...

  3. USACO maze1 BFS

    不写了很长的时间bfs该,很长一段时间的中间失误,当延期一次延伸成功的新节点的节点应该被标记为参观.否则,在某些情况下无限期延长队列. 输入一个小坑爹处理称号,能够进来当字符串被读取.然后用周围的墙上 ...

  4. ural 1932 The Secret of Identifier (容斥原理)

    标题效果: 计算到n字符串. 精确到只是有一个不同的字符,两个不同的字符.三个不同的字符,四对不同的字符. IDEAS: 枚举状态. dp[i] [j] ...当前串取出 i 状态下的全部字符转化成十 ...

  5. JAVA多线程两个实用的辅助类(CountDownLatch和AtomicBoolean)

    AtomicBoolean它允许一个线程等待一个线程完成任务,然后运行: A boolean value that may be updated atomically. See the java.ut ...

  6. Android: Receiving Data from the Send Intent,自己app注册系统分享

    当用户在系统的专辑,点击共享时.通过我们自己的app.分享此图片. 1.注册 主要是在AndroidManifest.xml中,对activity注冊Intent-filter.如: <acti ...

  7. DEV GridView嵌套

    近来的DEV搞更多.试图寻找专业点的程序做,对这样一个小小的研究. 本篇是多么真实,现在的记录,可以通过点击这条线的子表的内容相关联的行中打开的列表.的影响,如下面的: 以下是实现过程: 1.设计器里 ...

  8. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  9. 使用ExpandableListView时间轴效果达到

    不废话,首先在地图上,查看结果 这是用ExpandableListView来实现时间轴效果,原理比較简单,以月份为第一级,以天为第二级来实现的. package com.hj.main; import ...

  10. HDU 2647 Reward(图论-拓扑排序)

    Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...