【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. FFT快速傅里叶变化

    纪念人生第一次FFT 前排感谢iamzky,讲解非常详细 #include<iostream> #include<cstdio> #include<cmath> u ...

  2. MySQL数据库主从切换脚本自动化

    MySQL数据库主从切换脚本自动化 本文转载自:https://blog.csdn.net/weixin_36135773/article/details/79514507 在一些实际环境中,如何实现 ...

  3. mac Parallels Desttop 13 win7/win8 无法连接网络

    把 “源” 从共享网络改为"Wi-Fi" 在mac 这边点击菜单栏windows图标,选配置(如果没有配置,点控制中心,在点控制中心的齿轮,)选 硬件 找到网络 解锁,上边第一行就 ...

  4. 如何用纯 CSS 创作一个文本淡入淡出的 loader 动画

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

  5. HTML5一些特殊效果分享地址集合

    页面预加载图片原生js: http://www.cnblogs.com/st-leslie/articles/5274568.html HTML5 FileReader读取本地文件: http://n ...

  6. OverflowError:django signed integer is greater than maximum

    在学习一对一查询的时候,打印作者的电话时报了这个错 alex = Author.objects.filter(name='alex').first() print(alex.authordetail. ...

  7. Cleaning Shifts POJ - 2376 (贪心题)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31194   Accepted: 7677 ...

  8. BZOJ 4369: [IOI2015]teams分组

    把一个人看成二维平面上的一个点,把一个K[i]看成左上角为(0,+max),右下角为(K[i],K[i])的一个矩阵,那么可以很好地描述人对于询问是否合法(我也不知道他怎么想到这东西的) 然后把一组询 ...

  9. php生成Exeple表demo

    <?php require "./PHPExcel.php"; $dir=dirname(__FILE__); $objPHPExcel=new PHPExcel(); $o ...

  10. TensorFlow——深入MNIST

    程序(有些不甚明白的地方改日修订): # _*_coding:utf-8_*_ import inputdata mnist = inputdata.read_data_sets('MNIST_dat ...