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() == 0)return 1;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 = 1; it <= *it_max; ++it) {if (it < *it_min || (it > 0 && !vec[it - *it_min]))return it;}    return *it_max > 0 ? *it_max + 1 : 1;}private:
};

代码2(直接在原数组上进行交换, 空间代价为o(1))

class Solution {
public:int firstMissingPositive(vector<int>& nums) {if (nums.empty())return 1;auto begin = getFirstPositivePos(nums);if (*begin < 0)return 1;for (auto it = begin; it != nums.end(); ) {auto realPos = begin + *it - 1;if (realPos < nums.end() && realPos != it && *realPos != *it)iter_swap(it, realPos);else++it;}int index = 1;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()-1;while (true) {while (first < last && *first <= 0 ) ++first;while (first < last && *last > 0)--last;if (first < last)iter_swap(first, last);    elsebreak;}return first;}
};

转载于:https://www.cnblogs.com/lysuns/p/4479212.html

leetcode problem 41 -- First Missing Positive相关推荐

  1. LeetCode题解41.First Missing Positive

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

  2. 【leetcode】41. First Missing Positive

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

  3. 【排序+难题】LeetCode 41. First Missing Positive

    LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...

  4. [LeetCode]41.First Missing Positive

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

  5. leetcode 41. First Missing Positive 1

    题目要求 Given an unsorted integer array, find the first missing positive integer.For example, Given [1, ...

  6. leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

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

  7. 41. First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1, ...

  8. [leetcode]41. First Missing Positive

    题目地址 https://leetcode.com/problems/first-missing-positive/ 题目大意 一个整数数组,里面数字是无序的,在O(n)的时间复杂度,O(1)的空间复 ...

  9. leetcode 41. First Missing Positive

    https://www.cnblogs.com/grandyang/p/4395963.html https://www.jianshu.com/p/cf82ce91dc3d 错误解法1: [1,1] ...

  10. 41. First Missing Positive 缺失的第一个正数

    Title 给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [ ...

最新文章

  1. 网络口协商_以太网端口协商解析
  2. 零基础python从入门到精通 pdf-跟老齐学Python从入门到精通.pdf
  3. 论面向组合子程序设计方法 之 oracle
  4. 简单的MongoDB实践
  5. 前端学习(2690):重读vue电商网站11之使用树形控件 el-tree
  6. 领域应用 | 知识图谱数据构建的“硬骨头”,阿里工程师如何拿下?
  7. ini_set ini_get 可操作配置参数列表 设置默认编码等
  8. C#开发笔记,点点细微,处处真情,记录开发中的难言之隐
  9. Windows系统服务相关的端口
  10. 图片php木马制作教程,图片木马制作大法
  11. python就业班-淘宝-目录.txt
  12. 吃萝卜去口气(口臭)[转]
  13. 格鲁夫12年前惊人的预言,解释了今天美国对中国芯片所做的一切
  14. PDF文件如何转成HTML格式?说一种思路
  15. 【无标题】C语言连续输出输入语句执行跳过的问题
  16. linux7安装网卡驱动,CentOS 7 安装无线网卡驱动
  17. 洛伦兹函数(Lorentzian Function)
  18. ubuntu下ns2-allinone-2.34…
  19. 半导体器件基础08:MOS管结构和原理(2)
  20. 【PHP】配置环境变量,查看php版本(保姆级图文)

热门文章

  1. android删除重复照片,android 删除重复文件
  2. 计算机系统结构_计算机系统基础:总线结构知识笔记
  3. docker 实践(十二)k8s 初体验
  4. SPOJ Problem 2178:He is offside!
  5. SharePoint Foundation 2013安装-1:先决条件准备
  6. Bind 一些有趣的东西 【未完待续】
  7. 在云端飞舞,遨游云浏览器体验感受
  8. 随笔之生成简单的验证码
  9. 【论文】Realtime lane tracking of curved local road 检测和跟踪功能
  10. 最小化——最速下降法matlab实现