【LeetCode】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.

找到第一个没有出现的正整数

思路:

http://tech-wonderland.net/blog/leetcode-first-missing-positive.html

就是把出现的正整数a都放在a-1的位置,然后从头开始历遍,发现A[i] != i + 1的情况就是所要的结果

当我们遍历数组的时候,如果我们发现A[i] != i,那么我们就swap(A[A[i]], A[i]),让A[i]放在正确的位置上。而对于交换之后的A[i],我们继续做这个操作,直至交换没有意义为止。没有意义表示当前数不是正数,或超过数组长度,或与A[A[i]]相等。我们不关心这些数被排在什么位置。此外还要考虑如果整个数组都是连续的正数,比如A[] = {1,2},经过我们上面的排序之后会变成{2, 1},因为A[1] == 1(从1开始对比),而A[2]超出范围,所以函数会认为2之前的都出现过了而2没有出现过,返回2。为了防止把正确的数"挤"出数组,我们让A[A[i]-1]与A[i]交换,然后比较i+1和A[i]。

代码如下:

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

【leetcode】 First Missing Positive的更多相关文章

  1. 【leetcode】First Missing Positive

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

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

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

  3. 【LeetCode】163. Missing Range

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...

  4. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  5. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  6. 【leetcode】1237. Find Positive Integer Solution for a Given Equation

    题目如下: Given a function  f(x, y) and a value z, return all positive integer pairs x and y where f(x,y ...

  7. 【LeetCode】163. Missing Ranges 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  8. 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  9. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

随机推荐

  1. PAT 乙级 1051

    题目 题目地址:PAT 乙级 1051 思路 最近做题发现一个比较明显的现象——总是在做简单题的过程中出现这样那样的小问题,究其原因我认为还是有很多细节性的知识没有掌握,这是在以后的学习过程中需要注意 ...

  2. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)

    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...

  3. Voyager的安装及配置文件

    使用代理服务器安装laravel http_proxy=http://localhost:1080 composer create-project --prefer-dist laravel/lara ...

  4. 如何用 CSS 和 D3 创作旋臂粒子动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xJrOqd 可交互视频 ...

  5. jQuery plugin : bgStretcher 背景图片切换效果插件

    转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...

  6. Python正则表达式详解——re库

    一.简介 1.1.相关链接 官方文档: Python2:https://docs.python.org/2/library/re.html Python3:https://docs.python.or ...

  7. Applied Nonparametric Statistics-lec5

    今天继续two-sample test Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/6 Mann ...

  8. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  9. Python虚拟机中的一般表达式(一)

    在Python虚拟机框架这一章中,我们通过PyEval_EvalFrameEx看到了Python虚拟机的整体框架.而这章开始,我们将了解Python虚拟机是如何完成对Python的一般表达式的执行,这 ...

  10. Jenkins自动化搭建测试环境(一)

    Jenkins基础 首先上官网jenkins.io上下载最新的Jenkins war包 将下载完成的war包解压 java -jar jenkins.war 接下来使用浏览器访问localhost:8 ...