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的更多相关文章

  1. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

  2. LeetCode题解41.First Missing Positive

    41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...

  3. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  4. LeetCode OJ 41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  5. 【LeetCode】41. First Missing Positive (3 solutions)

    First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...

  6. 【leetcode】41. First Missing Positive

    题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...

  7. LeetCode - 41. First Missing Positive

    41. First Missing Positive Problem's Link ---------------------------------------------------------- ...

  8. [array] leetcode - 41. First Missing Positive - Hard

    leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...

  9. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

随机推荐

  1. Ubuntu字体设置

    Ubuntu所带的字体不是很好看,比较模糊,现修改为微软雅黑 Win7安装分区下的 \windows\fonts\文件夹下,复制msyh.ttf和msyhhd.ttf到/home/m/msfonts文 ...

  2. 17周 oj 比較大小 类模板

    /*声明一个类模板,利用它分别实现两个整数. 浮点数和字符的比較,求出大数和小数. 说明:在类模板外定义各成员函数. 输入两个整数.两个浮点数和两个字符 从大到小输出两个整数.两个浮点数和两个字符 * ...

  3. 继续推广我的新博客xysay:http://www.xysay.com/

    RT 博客收拾了一下,准备以后就在那里记录论文笔记啦,求交流,求推荐,求友链~~~ http://www.xysay.com/

  4. ProgressSeekBar

    ProgressSeekBar.rar

  5. Linux HugePages及MySQL 大页配置

    http://blog.csdn.net/dba_waterbin/article/details/9669929       ㈠ HugePages简介             HugePages是 ...

  6. node.js 针对不同的请求路径(url) 做出不同的响应

    边看这个边写的: http://wenku.baidu.com/link?url=C4yLe-TVH6060u_x4t34H3Ze8tjoL7HjJaKgH-TvHnEYl-T_gAMYwhmrCeM ...

  7. [Effective C++ --011]在operator=中处理“自我赋值”

    一.何谓“自我赋值”? 1.1.场合一 直接赋值 w = w; 1.2.场合二 同一数组         a[i] = a[j]: 1.3.场合三 指针         *px = *py: 1.4. ...

  8. 标准I/O库之缓冲

    标准I/O库提供缓冲的目的是尽可能减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的麻烦. 标准I/O提供了三种类型的缓冲: (1) ...

  9. mysql事务问题

    mysql事务: 若mysql 开启事务后START TRANSACTION ,不显示提交commit,则默认自动回滚,而不是默认自动提交.

  10. ajax检查用户名

    Ajax实现的效果 究竟Ajax能实现什么功能呢?今天下午学习了一下Ajax,现在跟大家分享一下我的学习心得.Ajax是什么?工作机制又是什么?可能不大准确,只是我个人看了视频学习后的一点点看法. A ...