【题目】

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.

【分析】

类似于桶排序。

每当A[i] != i+1时,即A[i]不在正确的位置上,需要交换到排序数组中他相应的位置上去。

交换A[i]与A[A[i]-1],直到无法交换。

【代码】

/*********************************
*   日期:2015-01-13
*   作者:SJF0115
*   题目: 41.First Missing Positive
*   来源:https://oj.leetcode.com/problems/first-missing-positive/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
using namespace std;class Solution {
public:int firstMissingPositive(int A[], int n) {if(A == NULL || n <= 0){return 1;}//if// 交换到在排序数组中应有的位置for(int i = 0;i < n;i++){// 交换直到不能交换while(A[i] != i+1){// 是否需要交换if(A[i] <= 0 || A[i] > n || A[i] == i+1 || A[i] == A[A[i]-1]){break;}//if// 交换int tmp = A[i];A[i] = A[tmp-1];A[tmp-1] = tmp;}//while}//for// First Missing Positivefor(int i = 0;i < n;i++){if(A[i] != i+1){return i+1;}//if}//forreturn n+1;}
};int main(){Solution solution;int A[] = {1,1};cout<<solution.firstMissingPositive(A,2)<<endl;return 0;
}

【分析二】

题目的最后一行,要求O(n)实际上暗示了用hash,但是又说要contant space,就没法再开新空间来建hash。
正好这个题目中处理的是1到n的数据,提供了一个将输入的数组同时用作hash表的可能性。
于是算法就是:

  1. 第一遍扫描排除所有非正的数,将它们设为一个无关紧要的正数(n+2),因为n+2不可能是答案
  2. 第二遍扫描,将数组作为hash表来使用,用数的正负来表示一个数是否存在在A[]中。
    当遇到A[i],而A[i]属于区间[1,n],就把A中位于此位置A[i] – 1的数置翻转为负数。
    所以我们取一个A[i]的时候,要取它的abs,因为如果它是负数的话,通过步骤一之后,只可能是我们主动设置成负数的
  3. 第三遍扫描,如果遇到一个A[i]是正数,说明i+1这个数没有出现在A[]中,只需要返回即可。
  4. 上一步没返回,说明1到n都在,那就返回n+1

【代码二】

class Solution {
public:int firstMissingPositive(int A[], int n) {if(A == NULL || n <= 0){return 1;}//ifint impossValue = n + 2;//first run, turn every negetive value into an impossible positive value//make every value in A is positivefor(int i = 0;i < n;i++){if(A[i] <= 0){A[i] = impossValue;}//if}//for//second run, make A[] as a hash table, A[i] indicate the presence of i + 1//the way is that, if k in [1,n] is in A[], then turn A[k -1] to negetivefor(int i = 0;i < n;i++){int value = abs(A[i]);if(value <= n){A[value-1] = -abs(A[value-1]);}//if}//for//third run, if A[i] is positive, from step 2, we know that i + 1 is missing.for(int i = 0;i < n;i++){if(A[i] > 0){return i+1;}//if}//for//all int from 1 to n is present, then return n + 1return n+1;}
};

[LeetCode]41.First Missing Positive相关推荐

  1. 【排序+难题】LeetCode 41. First Missing Positive

    LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...

  2. leetcode 41. First Missing Positive 1

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

  3. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

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

  4. [leetcode]41. First Missing Positive

    题目地址 https://leetcode.com/problems/first-missing-positive/ 题目大意 一个整数数组,里面数字是无序的,在O(n)的时间复杂度,O(1)的空间复 ...

  5. leetcode 41. First Missing Positive

    https://www.cnblogs.com/grandyang/p/4395963.html https://www.jianshu.com/p/cf82ce91dc3d 错误解法1: [1,1] ...

  6. LeetCode题解41.First Missing Positive

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

  7. LeetCode 41. First Missing Positive--Python 解法--数学题-找到不存在的最小正整数-O(1)空间复杂度

    题目地址:First Missing Positive - LeetCode Given an unsorted integer array, find the smallest missing po ...

  8. 41. First Missing Positive

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

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

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

最新文章

  1. es6结构赋值--数组
  2. linux命令应用之一
  3. IOS UITableView 加载未知宽高图片的解决方案
  4. 游戏中用到的设计模式
  5. 【Linux】一步一步学Linux——clear命令(241)
  6. C/C++之内存对齐
  7. 代码审查“思维导图”
  8. LeetCode 966. 元音拼写检查器(哈希)
  9. 变频器基础:变频器工作原理与常用功能
  10. 计算机行业未来作文,未来的计算机作文
  11. Python入门基础-七、案例4 52周存钱挑战 #列表(list)#math 库#for循环#range()用法#函数的参数传递#datetime库
  12. mac sublime中文乱码问题解决
  13. 对接微信二维码支付(native方式)
  14. 2018我们讲一下百度云BAE专业引擎的使用
  15. Python爬虫基础讲解(二十七):scrapy 框架—ltem和scrapy.Request
  16. 协同办公风口来袭,在线文档顺势崛起!
  17. 【编程学习】浅谈哈希表及用C语言构建哈希表!
  18. 中国国有资本投资运营公司“十四五”投资规划及发展动向展望报告2022-2028年
  19. usb_register_dev
  20. ST 电机控制工作台帮助文档翻译 之 使用 ST 电机控制工作台(工作台(帮助菜单命令))

热门文章

  1. oracle什么时候使用静态监听,Oracle监听之动态监听与静态监听特点
  2. html5历史纪录管理,HTML 5 新增方法以及历史管理
  3. 航空航天工程用不用学c语言,2020年北京航空航天软件工程991答疑
  4. python服务器运维书_python自动化运维书
  5. excel处理几十万行数据_神奇吗?仅需4步,轻松在Excel处理300万行数据
  6. python中flush什么意思,Python的file.flush()到底在做什么?
  7. html刘海屏高度,iphone刘海屏网页适配方法
  8. std::list 循环删除指针_数据结构_006_线性表_循环链表
  9. 细胞亚器文献阅读之酵母液泡与线粒体的动态互作A Dynamic Interface between Vacuoles and Mitochondria in Yeast
  10. 新出版书籍《Python预测之美:数据分析与算法实战》,送书活动!参与即可机会,获得一本实体书,中奖后可填写地址寄送。