[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.
思路:新建一个数组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相关推荐
- LeetCode First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- LeetCode: First Missing Positive 解题报告
Q: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2 ...
- leetcode - First Missing Positive
题目描述:点击此处 利用到原来的数组空间,不知道符不符合题意. 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int ...
- 【排序+难题】LeetCode 41. First Missing Positive
LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...
- [LeetCode]41.First Missing Positive
[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1 ...
- leetcode 41. First Missing Positive 1
题目要求 Given an unsorted integer array, find the first missing positive integer.For example, Given [1, ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
最新文章
- python function if yield_Python中的yield关键字
- shopex还是ecshop
- 牛客网(剑指offer) 第二十一题 栈的压入、弹出序列
- python实战,教你用微信每天给女朋友说晚安
- 总结常见的ES6新语法特性
- Linux装好MATLAB无法启动的解决办法
- Kotlin入门(23)适配器的进阶表达
- 怎样把百度图片设置成传统翻页 瀑布流与传统翻页的转换
- 谷歌被墙,如何给谷歌浏览器添加迅雷下载插件
- 为了寻找当下最好的照片备份方式,我写了7000字的长文...
- Arduino GPS 车速表(Arduino流体力学燃油效率计)(更新:2022.7.3)
- 【ERP流程图】:生产制造
- 再添近10个新冠知识图谱,OpenKG发布第二批开放数据集
- 烽火2640路由器命令行手册-01-基础配置命令
- Linq分页(skip和Take)
- python宣传海报_用Python做一个令人发疯的海报
- java计算机毕业设计员工婚恋交友平台源码+数据库+系统+lw文档+部署
- Veusz教程(1)——导入数据
- 苹果库乐队怎么玩_自制手机铃声(苹果)
- 迎接新时代,维谛技术全面呈献硬核策略
热门文章
- 自媒体这个行业看似门槛很低,但是赚到钱的人,也是少数,为什么呢?
- 关于直播带货被坑的厂商
- 富人为什么赚钱这么容易
- 修饰类方法(静态方法)
- 搭建属于自己的私有链,部署简单的智能合约
- php和asp渲染页面,Vue.js与 ASP.NET Core 服务端渲染功能
- java jint_Android使用JNI实现Java与C之间传递数据
- 图数据库 graph_通过SQL Server中的自连接了解Graph数据库相对于关系数据库的好处
- alwayson高可用组_AlwaysOn可用性组–如何在集群实例和独立实例之间设置AG(第1部分)
- azure 入门_Azure Function应用程序入门