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.

解法一:O(nlogn) time and O(1) space

无脑解法-->先排序O(nlogn)

思想如下:

1、略去非正的前缀数。

2、记下一个待匹配的正整数为tag。

考虑重复,如果A[i]等于tag,则tag++

如果A[i]大于tag,则返回tag

A[i]不可能小于tag,由排序可以保证。

class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
sort(A,A+n);
int i = ;
while(i < n && A[i] <= )
i ++;
if(i == n)
return ;
int tag = ;
for(; i < n; i ++)
{
if(A[i] > tag)
//miss the tag
return tag;
else if(A[i] == tag)
//next positive
tag ++;
else
;
}
//i==n, miss the tag
return tag;
}
};

解法二:O(n) time and O(n) space

稍作思考就可以知道,n容量的数组,最多覆盖的正整数为连续的1~n

也就是说,丢失的正整数要么出现在1~n中,要么就是n+1

因此可以构造大小为n的数组v,v[i]记录i+1这个数字是否出现在A中。

如果tag全true则返回n+1,否则返回第一个tag为负的下标+1

class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
//tag[i] means whether i+1 exists in A
//at most 1~n, then return n+1
vector<bool> tag(n, false);
for(int i = ; i < n; i ++)
{
if(A[i] > && A[i] <= n)
tag[A[i]-] = true;
}
for(int i = ; i < n; i ++)
{
if(tag[i] == false)
return i+;
}
return n+;
}
};

解法三:O(n) time and O(1) space

解法二中我们构建了新的数组tag来记录每个正整数是否出现在A中,

其实可以省略tag数组,直接在A中记录。这点借鉴了yuyibestman想法。

具体做法为,先将A划分为正整数与非负数。这点类似快排的partition。

A[i]的正负号记录i+1这个数字是否出现在A中。

由于只是符号取负,其值可以通过绝对值函数恢复。这样就代替了解法二中的tag数组了。

class Solution {
public:
int firstMissingPositive(int A[], int n) {
if(n == )
return ;
//if A[i] is negative, i+1 exists in original A //partition, non-negative only
int low = ;
int high = n-;
int end = n-;
while(low <= high)
{
while(low <= high && A[low] > )
low ++;
//to here,
//case low > high: partition finished, high point to the end of the new array
if(low > high)
{
end = high;
break;
}
//case A[low]<=0: low point to the first non-positive element while(high >= low && A[high] <= )
high --;
//to here,
//case high < low: partition finished, high point to the end of the new array
if(low > high)
{
end = high;
break;
}
//case A[high]>0: high point to the first positive element
swap(A[low],A[high]);
} for(int i = ; i <= end; i ++)
{
//check whether num is in A, and set A[num-1] to be negative
int num = abs(A[i]);
if(num <= end+)
{
if(A[num-] > )
A[num-] *= -;
}
//out of range 1~end+1
} for(int i = ; i <= end; i ++)
{
if(A[i] > )
return i+;
}
return end+;
}
};

最新文章

  1. 模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用A,B,C,D表示,当选某候选人时,直接输入其编号(编号由计算机随机产生,若输入的不是A,B,C,D则视为无效票,选举结束后按得票数从高到底输出候选人编号和所得票数.
  2. Liferay7 BPM门户开发之45: 集成Activiti文件上传部署流程BPMN模型
  3. Linux基本操作命令之文件查看cat more less tail head
  4. DCMTK3.6.0(MD支持库)安装说明
  5. mysql主从复制-linux版本
  6. Linux后台运行程序
  7. datasnap的前世今生
  8. Java write And read Demo
  9. web server性能优化浅谈
  10. Ireport5.0.1 从java后台接收list集合
  11. js中valueOf方法的使用
  12. js 向上和向下取整
  13. Vue-devtools安装步骤
  14. linux下 gdb+coredump 调试偶发crash的程序
  15. vue.js 入门学习
  16. Leetcode 记录(1~100)
  17. AI 卷积神经网络
  18. lua-resty-qless-web UI 界面运行
  19. django URLconf调度程序
  20. HP ALM lis

热门文章

  1. [MAC OS] XCode中的Debug View Hierarchy功能
  2. u-boot支持yaffs映像烧写的补丁
  3. 【BZOJ】【2595】【WC2008】游览计划
  4. MAC 10.10 开机登录无敌风火轮问题解决方式
  5. Spark一个简单案例
  6. Mysql数据库事务及隔离级别学习测试
  7. 第一章 EL表达式常见用法
  8. [leetcode]Insertion Sort List @ Python
  9. 理清Processor, Processor Sockets, Processor Cores, Logical Processors, Hyperthreading这些概念吧
  10. vue路由使用踩坑点:当动态路由再使用路由name去匹配跳转时总是跳转到根路由的问题