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.

思路:新建一个数组B,然后用A[i]的值作为B的index,即B[A[i]] =1,然后遍历一遍B,寻找B[i]!=1的index,不过貌似我的时间复杂度是O(n),空间也是O(n)啊。。

class Solution {public:int firstMissingPositive(int A[], int n)  {   vector<int> B;B.resize(n + 1); //cout << "n\t" << n << endl;//printArray(A, n);for(int i = 0; i < n; i++){   if(A[i] <= n && A[i] >0) {   // do A[i]'s value as B's indexB[A[i]] = 1;}   }   //printVector(B);for(size_t i = 1; i < B.size(); i++){   if(B[i] != 1)return i;}   return  n+1;}
};

为了实现O(n)的空间复杂度,只能使用数组A自身的空间,A[i]保存i+1,不停的交换,具体看注释吧,我觉得写的挺清楚的。。

class Solution {public:
#if 0int firstMissingPositive(int A[], int n) {vector<int> B;B.resize(n + 1);//cout << "n\t" << n << endl;//printArray(A, n);for(int i = 0; i < n; i++){if(A[i] <= n && A[i] >0){// do A[i]'s value as B's indexB[A[i]] = 1;}}//printVector(B);for(size_t i = 1; i < B.size(); i++){if(B[i] != 1)return i;}return  n+1;}
#endifint firstMissingPositive(int A[], int n){int i = 0;int tmp;// A[i] should store (i + 1)// it means A[0] stores 1, A[1] stores 2, ...while(i < n){// A[0~n-1] only can store 1~n;// A[i] should store i + 1// A[i] != i +1 menas A[i] should be move to others, but A[i] should be moved where?// A[i] should be move to A[i]-1// i.e.: {3, 1, 4, -1}// A[0]= 3, != (0+1), so 3 shoule be moved. moved to A[0] -1 =2, then check if A[2] and A[0] are equal.// so swap 3 and 4, then the array is {4, 1, 3 , -1}// but 4 also doesn't appear at A[0], so go on to swap, until the condition doens't meet, then i++if(A[i] <= n && A[i] >0 && A[i] != (i+1) && A[A[i] - 1] != A[i] ){tmp = A[i];A[i] = A[tmp - 1];A[tmp - 1] = tmp;}elsei++;}//printArray(A, n);for(int i = 0; i < n; i++){if(A[i] != (i+1) )return i + 1;}return n+1;}
};

转载于:https://www.cnblogs.com/diegodu/p/4285599.html

[LeetCode] First Missing Positive相关推荐

  1. LeetCode First Missing Positive

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

  2. LeetCode: First Missing Positive 解题报告

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

  3. leetcode - First Missing Positive

    题目描述:点击此处 利用到原来的数组空间,不知道符不符合题意. 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int ...

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

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

  5. [LeetCode]41.First Missing Positive

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

  6. leetcode 41. First Missing Positive 1

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

  7. LeetCode题解41.First Missing Positive

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

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

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

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

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

最新文章

  1. python function if yield_Python中的yield关键字
  2. shopex还是ecshop
  3. 牛客网(剑指offer) 第二十一题 栈的压入、弹出序列
  4. python实战,教你用微信每天给女朋友说晚安
  5. 总结常见的ES6新语法特性
  6. Linux装好MATLAB无法启动的解决办法
  7. Kotlin入门(23)适配器的进阶表达
  8. 怎样把百度图片设置成传统翻页 瀑布流与传统翻页的转换
  9. 谷歌被墙,如何给谷歌浏览器添加迅雷下载插件
  10. 为了寻找当下最好的照片备份方式,我写了7000字的长文...
  11. Arduino GPS 车速表(Arduino流体力学燃油效率计)(更新:2022.7.3)
  12. 【ERP流程图】:生产制造
  13. 再添近10个新冠知识图谱,OpenKG发布第二批开放数据集
  14. 烽火2640路由器命令行手册-01-基础配置命令
  15. Linq分页(skip和Take)
  16. python宣传海报_用Python做一个令人发疯的海报
  17. java计算机毕业设计员工婚恋交友平台源码+数据库+系统+lw文档+部署
  18. Veusz教程(1)——导入数据
  19. 苹果库乐队怎么玩_自制手机铃声(苹果)
  20. 迎接新时代,维谛技术全面呈献硬核策略

热门文章

  1. 自媒体这个行业看似门槛很低,但是赚到钱的人,也是少数,为什么呢?
  2. 关于直播带货被坑的厂商
  3. 富人为什么赚钱这么容易
  4. 修饰类方法(静态方法)
  5. 搭建属于自己的私有链,部署简单的智能合约
  6. php和asp渲染页面,Vue.js与 ASP.NET Core 服务端渲染功能
  7. java jint_Android使用JNI实现Java与C之间传递数据
  8. 图数据库 graph_通过SQL Server中的自连接了解Graph数据库相对于关系数据库的好处
  9. alwayson高可用组_AlwaysOn可用性组–如何在集群实例和独立实例之间设置AG(第1部分)
  10. azure 入门_Azure Function应用程序入门