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)。

public int firstMissingPositive(int[] nums) {
if (null == nums || nums.length == 0) {
return 1;
}
for (int i = 0; i < nums.length; i++) {
while (nums[i] > 0 && nums[i] != i + 1 && nums[i] <= nums.length) {
int temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return nums.length + 1;
}

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. Python3 tesseract加载chi_sim异常停止工作

    Python3 tesseract加载chi_sim异常停止工作 原因: chi_sim.traineddata 和 tesseract3.0.2 版本不一致: 解决方案: 下载tesseract3. ...

  2. getContext,getApplicationContext和this有什么区别

    使用this, 说明当前类是context的子类,一般是activity application等使用getApplicationContext 取得的是当前app所使用的application,这在 ...

  3. Linux内存管理--用户空间和内核空间【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/12045255 关于虚拟内存有三点需要注意: 4G的进程地址空间被人为的分为两个部分--用 ...

  4. 【分词器及自定义】Elasticsearch中文分词器及自定义分词器

    中文分词器 在lunix下执行下列命令,可以看到本来应该按照中文”北京大学”来查询结果es将其分拆为”北”,”京”,”大”,”学”四个汉字,这显然不符合我的预期.这是因为Es默认的是英文分词器我需要为 ...

  5. Planar Shadow

    Unity上平面阴影的计算与实现 //如何求顶点投影到平面上的点(阴影点) //当平面上取不相等的任意两个点组成一个向量,与平面的法线总是垂直的,向量垂直点乘为0,因此可以通过一个点和一个法线来定义, ...

  6. UVa 10048 噪音恐惧症(Floyd)

    https://vjudge.net/problem/UVA-10048 题意: 输入一个C个点S条边的无向带权图,边权表示该路径上的噪声值.输入一些询问,每次询问两个点,输出这两点间最大噪声值最小的 ...

  7. html js 捕捉鼠标右键事件,按下滚轮事件,左键点击事件

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. C#学习 day1 c#基础

    C#是一门编程语言,为什么我今天开始学C#了,下学期有门C#的课,以及有一个经验丰富的老学长正在做C#项目,由于之前学过C++和C基础,所以,C#的基础部分我查看文档来尝试能否自学归纳,而不是一直依靠 ...

  9. Codeforces 448E - Divisors

    448E - Divisors 思路: dfs.注意如果是1,直接返回,因为1的因子还是1. 因为x因子的因子还是x的因子,所以可以事先处理好x因子的因子在x因子中的位置. 不用这个方法也可以,用ma ...

  10. oracle创建/删除表空间、创建/删除用户并赋予权限

    创建表空间 分开执行如下sql语句 --创建临时表空间 CREATE SMALLFILE TEMPORARY TABLESPACE "TEMP11" TEMPFILE 'E:\ap ...