【题目描述】

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

给出一个无序的整数数组,找出其中没有出现的最小正整数。

【题目链接】

www.lintcode.com/en/problem/first-missing-positive/

【题目解析】

此题可用桶排序。桶排序通常需要按照一定规则将值放入桶中,一般需要额外的 O(n) 空间,咋看一下似乎不太适合在这道题中使用,但是若能设定一定的规则原地交换原数组的值呢?这道题的难点就在于这种规则的设定。

设想我们对给定数组使用桶排序的思想排序,第一个桶放1,第二个桶放2,如果找不到相应的数,则相应的桶的值不变(可能为负值,也可能为其他值)。

那么怎么才能做到原地排序呢?即若 A[i]=x, 则将 x 放到它该去的地方 - A[x−1]=x, 同时将原来 A[x−1] 地方的值交换给 A[i].

排好序后遍历桶,如果不满足 f[i]=i+1, 那么警察叔叔就是它了!如果都满足条件怎么办?那就返回给定数组大小再加1呗。

【答案】

www.jiuzhang.com/solutions/first-missing-positive/

转载于:https://www.cnblogs.com/foxli/p/8357980.html

Lintcode189 First Missing Positive solution 题解相关推荐

  1. LeetCode题解41.First Missing Positive

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

  2. 41. First Missing Positive

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

  3. 41-First Missing Positive

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

  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

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

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

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

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

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

  8. 【leetcode】First Missing Positive(hard) ☆

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

  9. [LeetCode]41.First Missing Positive

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

最新文章

  1. [力扣] 304. 二维区域和检索 - 矩阵不可变
  2. OS / Linux / Select 调用流程
  3. salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)
  4. 洛谷P1852:跳跳棋(LCA,树形结构)
  5. android开发入门_Android开发入门
  6. C++类构造函数初始化列表及初始化成员变量的误区
  7. 泰禾光电机器人研发_机器之眼 | 3D相机能让机器人看见什么?
  8. lcfirst_PHP lcfirst()函数与示例
  9. matlab size11,matlab学习笔记11_3高维数组操作 filp, shiftdim, size, permute, ipermute
  10. 支付宝COM组件浅析
  11. python绘制小狗_SAI教程:绘画小狗手绘板教程
  12. js 数组Array的常用方法 es3 es5 es6
  13. Google地图中关于根据具体坐标定位真实地理位置
  14. 单例模式 饿汉式与懒汉式
  15. QQ微派狼人杀经典秘籍进阶篇
  16. python 基于百度aip库 实现识别图片中的文字
  17. 第六章:个人主页和头像
  18. 日历控件CalendarView
  19. “文心一言”和“ChatGPT”两者有何差距?
  20. html中outline属性,css的outline属性的作用是什么

热门文章

  1. Oracle——15触发器
  2. KVM配置之(3)- 克隆
  3. 实战应对三种因素引起的交换故障
  4. 【数据挖掘笔记三】数据预处理
  5. Linux命令free解读
  6. 安装opencv3.0.0与配置Visual studio 2008
  7. python字符串合并去重_Python合并同类项的字符串,一对多
  8. Learn About Service Cloud for Agents
  9. 实验18:使用@Autowired注解实现根据类型实现自动装配★
  10. Spring(IOC+AOP)