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

Example 1:

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

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

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

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

思路:这个题刚開始是没有思路的,难就难在O(n)时间内常数量空间,所以此题较为考察思维敏捷性。其解题核心思想是将数组的第i位存正数i+1。最后再遍历一次就可以。

其它人的思想,我也是看了这个思想自己写的代码。

尽管不能再另外开辟很数级的额外空间,可是能够在输入数组上就地进行swap操作。

思路:交换数组元素。使得数组中第i位存放数值(i+1)。

最后遍历数组,寻找第一个不符合此要求的元素,返回其下标。整个过程须要遍历两次数组,复杂度为O(n)。

下图以题目中给出的第二个样例为例,解说操作过程。

妈蛋。这题挣扎好久。

首先思路上,其次临界条件,这题和以下题异曲同工:

n个元素的数组,里面的数都是0~n-1范围内的,求数组中反复的某一个元素。没有返回-1, 要求时间性能O(n) 空间性能O(1)。

  1. public int firstMissingPositive(int[] nums) {
  2. if (null == nums || nums.length == 0) {
  3. return 1;
  4. }
  5. for (int i = 0; i < nums.length; i++) {
  6. while (nums[i] > 0 && nums[i] != i + 1 && nums[i] <= nums.length) {
  7. int temp = nums[nums[i] - 1];
  8. nums[nums[i] - 1] = nums[i];
  9. nums[i] = temp;
  10. }
  11. }
  12. for (int i = 0; i < nums.length; i++) {
  13. if (nums[i] != i + 1) {
  14. return i + 1;
  15. }
  16. }
  17. return nums.length + 1;
  18. }

https://www.cnblogs.com/clnchanpin/p/6727065.html

[LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)的更多相关文章

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

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

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

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

  3. [leetcode]41. First Missing Positive第一个未出现的正数

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

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

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

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

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

  6. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

  7. LeetCode - 41. First Missing Positive

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

  8. leetcode 41 First Missing Positive ---java

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

  9. Java [Leetcode 41]First Missing Positive

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

随机推荐

  1. ”MySQL查询优化“学习总结

    查询优化有几种方法,下面分别介绍. 切分查询 一条大的语句(涉及很多行)一次会锁住很多数据(不利于高并发). 占满整个事务日志,耗尽系统资源.阻塞很多小的但很重要的查询. 分解关联查询 关联查询分解方 ...

  2. 20165310_获奖感想与Java阶段性学习总结

    获奖感想与Java阶段性学习总结 一.Learning By Doing ​ 在此之前,其实我并没有想到能够成为小黄杉的第一批成员之一,喜悦之余,也感受到了许多的压力.小黄杉一方面代表了老师对于我这一 ...

  3. Bof基础实践

    Bof基础 Bof原理 Linux下进程地址空间的布局 典型的堆栈结构 上图中可以看到栈中有return address还有局部变量,也就是函数的参数,bof攻击是利用上参数的溢出将返回地址retur ...

  4. bzoj 1010 玩具装箱toy -斜率优化

    P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具 ...

  5. [c/c++]指针(4)

    现在讲一下指针的主要用途和常见错误. [用途] 1.用作形参 首先,常见新手写交换函数: void swap(int a,int b){ int t = a; a = b; b = t; } 主函数里 ...

  6. 托管C++调用C#

    拿到了一个第三方demo,有dll,有.cpp..h,打开解决方案,如下图: 网上资料貌似很少,根据猜测: 这是使用托管C++来调用C#的方式. 过程: 1.先使用C#代码实现界面和功能,其实就是一个 ...

  7. HDU 1686 Oulipo(KMP)题解

    题意:主串中能找到几个模式串 思路:超详细解释KMP KMP:针对这个代码,解释一下Fail数组的含义:T为主串,P为模式串,Fail代表失配值,即当P[j] != T[i]时,j要指向的位置为Fai ...

  8. Bi-shoe and Phi-shoe(欧拉函数/素筛)题解

    Bi-shoe and Phi-shoe Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe ...

  9. HDU1560 DNA sequence(IDA*)题解

    DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  10. 4、CommonChunkPlugin提取公共js-提取多个

    cnpm install css-loader --save-dev    //css-loader 是将css打包进js cnpm install style-loader --save-dev   ...