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 ...
随机推荐
- 教你50招提升ASP.NET性能(四):精选的技巧
(4)A selection of tips 招数4: 精选的技巧 Make sure HTTP compression is turned on for any uncompressed conte ...
- Win8.1激活
激活查询:在桌面状态下输入“Win+R”,进入运行栏目,输入slmgr.vbs -dlv 显示:最为详尽的激活信息,包括:激活ID.安装ID.激活截止日期 http://www.nruan.com/w ...
- swift 开篇
苹果的WWDC ,除了公布了os x 10.10 和IOS8 外,还推出了Swift.具体点击这里 代码整体风格有点像Java,也有点像javascript. 以下给出一些代码段(来自苹果官方手冊): ...
- Clover
为您的 Windows Explorer 插上翅膀! Clover 是 Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能. 方便的 Tab ...
- 信号之sigsuspend函数
更改进程的信号屏蔽字可以阻塞所选择的信号,或解除对它们的阻塞.使用这种技术可以保护不希望由信号中断的代码临界区.如果希望对一个信号解除阻塞,然后pause等待以前被阻塞的信号发生,则又将如何呢?假定信 ...
- 10 Technologies That will Shape Future Education--reference
http://dizyne.net/technologies-that-will-shape-future-education/ Technology is on the rise and with ...
- iOS 常见知识点(三):Lock
iOS 常见知识点(一):Runtime iOS 常见知识点(二):RunLoop 锁是最常用的同步工具.一段代码段在同一个时间只能允许被有限个线程访问,比如一个线程 A 进入需要保护代码之前添加简单 ...
- 深入研究Block捕获外部变量和__block实现原理
Blocks是C语言的扩充功能,而Apple 在OS X Snow Leopard 和 iOS 4中引入了这个新功能“Blocks”.从那开始,Block就出现在iOS和Mac系统各个API中,并被大 ...
- JS类型(2)_JS学习笔记(2016.10.02)
undefined undefined是全局对象(window)的一个特殊属性,其值是未定义的.但 typeof undefined 返回 'undefined' . 虽然undefined是有特殊含 ...
- JS 笔记
如何定义一个函数呢?基本语法如下: function 函数名() { 函数代码; } 说明: 1. function定义函数的关键字. 2. "函数名"你为函数取的名字. ...