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. 找回丢失的SQL Server性能计数器

    There was one time when I was delivering a Service using a tool that gathers performance data throug ...

  2. BZOJ 2150: 部落战争 最大流

    2150: 部落战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php? ...

  3. NYOJ109 数列转换 【守恒法】

    数列转换 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 有一个数列a1,a2,a3...an,每次能够从中随意选三个相邻的数ai-1 ,ai , ai+1 ,进行例 ...

  4. Introdution to 3D Game Programming With DirectX11 第11章 习题解答

    11.1 这道题要注意使用了line strip,由于曾经一直用triangle list,所以在几何渲染的时候easy算错定点描绘的顺序. 贴一些代码,大概就能把这个问题解释清楚了,由于框架还不是特 ...

  5. Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)

    [目录] 一.[基础知识——扫盲] 二.[DOM.SAX.JDOM.DOM4j简单使用介绍] 三.[性能测试] 四.[对比] 五.[小插曲XPath] 六.[补充] 关键字:Java解析xml.解析x ...

  6. Android GridView 添加 网格线解决办法

    在使用 GridView 网格布局时,默认情况下 GridView  是没有网格线的,但是有些时候我们需要让GridView显示分割线,怎么办呢?查了不少资料,找到了一种为GridView添加网格线的 ...

  7. ios 入门之Hello World

    1.ios系统的概述与构架ios平台限制集成开发环境介绍第一个程序-hello World应用程序的文件组织模拟器的常用操作应用程序的生命周期 CocoaTouch层UIKit框架:UIKit提供了一 ...

  8. JAVA_eclipse 保留Java文件时自动格式化代码和优化Import

    Eclipse 保存Java文件时自动格式化代码和优化Import Eclipse中format代码的快捷方式是ctrl+shift+F,如果大家想保存 java文件的时候 自动就格式化代码+消除不必 ...

  9. Android 自学之绝对布局 AbsoluteLayout

    绝对布局(AbsoluteLayout),绝对布局就像java AWT中的空布局:所谓的绝对布局就是Android不提供任何的布局控制,而是有开发人员自己通过X坐标和Y坐标来控制组件的位置.当使用绝对 ...

  10. CentOS7安装和配置FTP

    1. 安装vsftpd #安装vsftpd yum install -y vsftpd #设置开机启动 systemctl enable vsftpd.service # 重启 service vsf ...