leetcode problem 41 -- 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.
题意:
给定了一个无序数组,要求找到第一个缺失的正整数.如1,2,4,5的第一个缺失的正整数是3.要求时间复杂度为o(n) 空间复杂度为o(1)
思路:
第一种是排序,时间复杂度为O(nlgn) 不行
第二种是hash查找 对每一个数字都用额外空间找到正确的位置. 然后一次遍历找到第一个缺失的正整数. 时间复杂度为O(n) 但是空间复杂度为O(n) 也不符合题目要求
第三种直接在输入数组上进行交换操作, 使得每个数字通过交换能在正确的位置上.如3,1,7,2 这组数字第一次交换后得到7,1,3,2 数字3放置在了正确的位置.
这种时间复杂度为O(n) 而且只需要常数辅助空间. 但我认为这种方法依然不复合题目要求,因为它破坏了原始数组. 它其实也类似于hash, 需要O(n)的空间,只不过这O(n)的空间借用了原始输入数组上的.
目前我还没找到即不破坏原始数组,空间复杂度又为O(1)的方法
代码1(hash法):
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
if (nums.size() == )
return ;
auto it_max = std::max_element(nums.begin(), nums.end());
auto it_min = std::min_element(nums.begin(), nums.end());
vector<bool> vec(*it_max - *it_min, false);
for (auto elem : nums)
vec[elem - *it_min] = true;
for (auto it = ; it <= *it_max; ++it) {
if (it < *it_min || (it > && !vec[it - *it_min]))
return it;
}
return *it_max > ? *it_max + : ;
}
private:
};
代码2(直接在原数组上进行交换, 空间代价为o(1))
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
if (nums.empty())
return ;
auto begin = getFirstPositivePos(nums);
if (*begin < )
return ;
for (auto it = begin; it != nums.end(); ) {
auto realPos = begin + *it - ;
if (realPos < nums.end() && realPos != it && *realPos != *it)
iter_swap(it, realPos);
else
++it;
}
int index = ;
for (auto it = begin; it != nums.end(); ++it, index++) {
if (index != *it)
return index;
}
return index;
}
private:
vector<int>::iterator getFirstPositivePos(vector<int>& nums) {
auto first = nums.begin();
auto last = nums.end()-;
while (true) {
while (first < last && *first <= )
++first;
while (first < last && *last > )
--last;
if (first < last)
iter_swap(first, last);
else
break;
}
return first;
}
};
leetcode problem 41 -- First Missing Positive的更多相关文章
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- 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
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
随机推荐
- Xcode中c++&Object-C混编,详细介绍如何在cocos2dx中访问object函数以及Apple Api
转自:http://www.himigame.com/iphone-cocos2dx/743.html Cocos2dx系列博文的上一篇详细介绍了如何在Xcode中利用jni调用Android的Jav ...
- cocos2d-x 纹理研究
转自:http://blog.csdn.net/qq51931373/article/details/9119161 1.通常情况下用PVR格式的文件来进行图片显示的时候,在运行速度和内存消耗方面都要 ...
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
- dtrace4linux
http://www.oschina.net/p/dtrace4linux?fromerr=ZSxqzDcE
- There is no Action mapped for action name XXX. - [unknown location]
今天被这个问题费了不少时间,原因是缺少了 struts2-json-plugin-2.3.1.2.jar 包 当然,有时候也可能是缺少其他包, 把这个包添加到lib文件夹后还要刷新,clean一下,因 ...
- chrome浏览器更新到chrome 29.0.1547.76 m,多出一些蛋疼的功能来。
更新到chrome 29.0.1547.76 m 的时候,莫名其妙多出一些蛋疼的功能来. 1.alert之类的弹出对话框样式变了,并且位置不是居中的,跑到了最上面去了,如下图. 要把这对话框改回原始状 ...
- Eclipse 各种包说明
2001年11月7日 ,Eclipse 1.0发布 半年之后,2002年6月27日Eclipse进入了2.0时代.2.0时代的Eclipse经历了2.0和2.1两个大的版本.其中2.0在 之后又推出了 ...
- Helpers\Assets
Helpers\Assets The assets helper is for loading CSS and JS files rather than writing out the full sc ...
- linux 学习笔记 GNU工具链简介
我们通常无法直接通过Linux内核,而需要借助Linux内核之上的GUN工具链来进行 文件处理 文本操作 进程管理 等操作. GNU/Linux shell为用户提供了 启动程序 管理文件系统上的文件 ...
- Spring中BeanPostProcessor
Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...