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.
给出一组数,找到里面缺少的第一个正数。
让o(n)时间,而且是o(1)空间。
一开始想的是记录正数的个数和他们的和,利用1~n的公式求和,减去sum,看缺了哪个。
指导提交错误,发现居然可以输入 [1,1]。原来还可以有重复的。
想到应该是利用了原来的数组。
用原来的数组做哈希。
对于每一个数,把它放到它应该在的位置上。比如一个4,把它放到a[3]的位置上。
原来的数怎么办呢?调换。a[3]原来的数就放在a[i]的位置。反复调换,反复调换。当然其中有坑的,我提交了三次才AC了。
下面代码的执行效果就是:
比如输入 -3 1 5 3 2
index 0 1 2 3 4
————————————————
i = 0: -3 1 5 3 2 负值跳过了
i = 1: 1 -3 5 3 2 swap(-3,1)
i = 1: 1 -3 5 3 2 负值跳过
i = 2: 1 -3 3 swap(5,2)
i = 2: 1 2 -3 3 5 swap(2,-3)
i = 2: 1 2 -3 3 5 负数跳过
i = 3: 1 2 3 -3 5 swap(-3,3)
i = 4: 1 2 3 -3 5 负数跳过
最后变成了 1 2 3 -3 5
很明显,缺的是4啦。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int i,j,n,tmp;
vector<int> &a = nums;
n = a.size(); if(n == ) return ; for(i = ,j = ; i < n; i++)
{
if(a[i] > && a[i] != i + ) //一个正数不在它自己的位置上,
{
tmp = a[i] - ; //tmp是它应该呆的位置
if(tmp < n && a[tmp]!= a[i]) //防止下标越界和 死循环
{
swap(a[tmp],a[i]);
i--;
} //这个调换最多会有多少次呢? 最多也就是n次。因为n次以后,n个数都会在正确的位置了。
}
}
for(i = ; i<n; i++) //数一数,看看中间却了哪个呢?如果都不缺,那就是1..n的连续数组,就返回 n + 1
{
if(a[i] != i + )
break;
} return i + ;
}
};
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] ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 【leetcode】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] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
随机推荐
- NodeJs使用Express框架开发时的快速调试方法
习惯了php开发,可以直接使用echo或者var_dump()将想要查看的变量结果输出到网页查看,非常的方便.但是使用express开发时,每次修改文件后,都需要使用npm start命令重启服务,然 ...
- 廖雪峰Java3异常处理-2断言和日志-4使用Log4j
1.Log4j Log4j是目前最流行的日志框架.有两个版本 1.x:Log4j 2.x:Log4j2 Log4j下载地址https://www.apache.org/dyn/closer.lua/l ...
- Kubernetes报错Failed to get system container stats for "/system.slice/kubelet.service"
tail -f /var/log/message Nov 14 07:12:51 image kubelet: E1114 07:12:51.627782 3007 summary.go:92] ...
- idea关闭标签快捷键修改----兼 常用实用快捷键
还有一个快捷键: 自动补全返回值 : ctrl + alt + v alt + enter: 自动添加未定义的方法 idea 原本的关闭快捷键是: ctrl + F4,但是不好用,谁的手指伸这么长 修 ...
- Java基础:IO流之字节流和字符流
1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...
- HTTP RFC解析
HTTP协议(HyperText Transfer Protocol,超文本传输协议)HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出, ...
- Android多线程下载
所用知识点: 1.设置http协议字段Range “bytes=“start+”-”+end conn.addRequestProperty("Range", "byte ...
- 一次线上zabbix server 挂掉的思考
突然间发现zabbix 挂了,咋发现的呢?报警的世界突然安静了,你就会觉得不妥了.这是运维人员的通病,有报警嫌烦,没报警心里会不安.1,图形界面上确实显示zabbix server is not ru ...
- Epic Games工程师分享:如何在移动平台上做UE4的UI优化?
转自:https://blog.csdn.net/debugconsole/article/details/79281290 随着技术的不断升级,高性能的引擎逐渐受到越来越多研发商的青睐,UE4就是其 ...
- vue808
自定义键盘信息: Vue.directive('on').keyCodes.ctrl=17; Vue.directive('on').keyCodes.myenter=13; 数据深度监听 ...