Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

  1. Input: [1,2,0]
  2. Output: 3

Example 2:

  1. Input: [3,4,-1,1]
  2. Output: 2

Example 3:

  1. Input: [7,8,9,11,12]
  2. Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

这道题让我们找缺失的首个正数,由于限定了 O(n) 的时间,所以一般的排序方法都不能用,最开始博主没有看到还限制了空间复杂度,所以想到了用 HashSet 来解,这个思路很简单,把所有的数都存入 HashSet 中,然后循环从1开始递增找数字,哪个数字找不到就返回哪个数字,如果一直找到了最大的数字(这里是 nums 数组的长度),则加1后返回结果 res,参见代码如下:

解法一:

  1. // NOT constant space
  2. class Solution {
  3. public:
  4. int firstMissingPositive(vector<int>& nums) {
  5. unordered_set<int> st(nums.begin(), nums.end());
  6. int res = , n = nums.size();
  7. while (res <= n) {
  8. if (!st.count(res)) return res;
  9. ++res;
  10. }
  11. return res;
  12. }
  13. };

但是上面的解法不是 O(1) 的空间复杂度,所以需要另想一种解法,既然不能建立新的数组,那么只能覆盖原有数组,思路是把1放在数组第一个位置 nums[0],2放在第二个位置 nums[1],即需要把 nums[i] 放在 nums[nums[i] - 1]上,遍历整个数组,如果 nums[i] != i + 1, 而 nums[i] 为整数且不大于n,另外 nums[i] 不等于 nums[nums[i] - 1] 的话,将两者位置调换,如果不满足上述条件直接跳过,最后再遍历一遍数组,如果对应位置上的数不正确则返回正确的数,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int firstMissingPositive(vector<int>& nums) {
  4. int n = nums.size();
  5. for (int i = ; i < n; ++i) {
  6. while (nums[i] > && nums[i] <= n && nums[nums[i] - ] != nums[i]) {
  7. swap(nums[i], nums[nums[i] - ]);
  8. }
  9. }
  10. for (int i = ; i < n; ++i) {
  11. if (nums[i] != i + ) return i + ;
  12. }
  13. return n + ;
  14. }
  15. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/41

类似题目:

Missing Number

Find the Duplicate Number

Find All Numbers Disappeared in an Array

Couples Holding Hands

参考资料:

https://leetcode.com/problems/first-missing-positive/

https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c++-solution-O(1)-space-and-O(n)-time

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] First Missing Positive 首个缺失的正数的更多相关文章

  1. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  2. 041 First Missing Positive 第一个缺失的正数

    给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...

  3. LeetCode OJ:First Missing Positive (第一个丢失的正数)

    在leetCode上做的第一个难度是hard的题,题目如下: Given an unsorted integer array, find the first missing positive inte ...

  4. Leetcode First Missing Positive

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

  5. LeetCode: First Missing Positive 解题报告

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

  6. LeetCode – First Missing Positive

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

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

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

  9. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

随机推荐

  1. [转] 给ubuntu中的软件设置desktop快捷方式(以android studio为例)

    原文链接:http://www.cnblogs.com/kinyoung/p/4493472.html ubuntu的快捷方式都在/usr/share/applications/路径下有很多*.des ...

  2. 用Github pages搭建自己制作的网页,方法最简单,适用于新手

    本文固定链接http://blog.csdn.net/pspgbhu/article/details/51205264 本人自学前端一个多月,写个几个网页想要用来应聘,网上搜各种搭建网站的方法,发现不 ...

  3. ASP.NET Core 中文文档 第二章 指南(4.10)检查自动生成的Detail方法和Delete方法

    原文 Examining the Details and Delete methods 作者 Rick Anderson 翻译 谢炀(Kiler) 校对 许登洋(Seay).姚阿勇(Mr.Yao) 打 ...

  4. Oracle 中的sql函数以及分页

    SELECT LPAD(,'*.') "LPAD example" FROM DUAL; 1.分页查询 (1)方法一:使用  between  and 来实现分页 select * ...

  5. WebAPI接收JSON参数注意事项

    运行环境:ASP.NET 4.5.2. 当我们向GlobalConfiguration.Configuration.MessageHandlers添加一个DelegatingHandler派生类后,很 ...

  6. 【趣味分享】C#实现回味童年的24点算法游戏

    一.24点游戏玩法规则效果展示 1.初始化界面 2.开始游戏界面 3.游戏超时界面 4.查看答案界面 5.答对界面 6.答错界面 7.计算表达式的验证界面 8.一副牌算完开始新一副牌界面 到这里24点 ...

  7. C# 系统托盘图标

    C# 系统托盘图标 WPF NotifyIcon 资料 网址: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon http://www. ...

  8. 四种解析和创建方式(DOM,SAX,DOM4J,JDOM)

    一.先导入jar包 DOM基于树形,SAX基于事件,DOM4J和JDOM基于底层API 二.代码如下 1 package com.sxt.test; import java.io.File; impo ...

  9. GJM : Unity3D HIAR -【 快速入门 】 七、使用本地识别包

    使用本地识别包 本文将向您介绍如何在 Unity 工程中使用本地识别包. Step 1.下载本地识别包 前往 HiAR 管理后台,上传您需要识别的图片并下载识别包,您可以获得一个 unitypacka ...

  10. spider RPC框架的需求来源与特性介绍(一)

    spider RPC 特性介绍 spider RPC 性能测试 spider RPC 入门指南 spider RPC 配置文件参考 spider RPC 开发指南 spider RPC 安全性 spi ...