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开始的正整数,第一个没有在所给数组中出现的正整数。

因为是从1开始的,我们可以将数组中所有的正整数都移动到它的正确位置,当所有数字都移动完毕后,从头检查第一个没有出现在该出现位置上的正整数就是所求的数字。若所有的数都出现了,则所求的数字就是n + 1,所有数字后面的那个数字。

该算法因为所有数字都是移动到自己的正确位置,因此每个数字都只会被移动一次。复杂度因此是O(n)。

所给数组中如果出现了重复的数字也不要紧,因为随着交换,所有正整数都回到了自己的正确位置后,重复数字会被交换到剩余的位置中去。而这些位置并不是他们的正确位置,因此也能指示出他们所处位置的正数缺失了。

 class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = ; i < n; i++)
while (nums[i] > && nums[i] <= n && nums[nums[i] - ] != nums[i])
swap(nums[i], nums[nums[i] - ]);
for (int i = ; i < n; i++)
if (nums[i] != i + )
return i + ;
return n + ;
}
};

First Missing Positive -- LeetCode的更多相关文章

  1. First Missing Positive leetcode java

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

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

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

  3. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  4. leetcode 41 First Missing Positive ---java

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

  5. [Leetcode][Python]41: First Missing Positive

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...

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

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

  7. 【一天一道LeetCode】#41. First Missing Positive

    一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...

  8. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

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

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

随机推荐

  1. ssm项目中ueditor富文本编辑器的使用

    一.下载 https://ueditor.baidu.com/website/index.html 将ueditor放到项目中合适的位置 二 . 配置文件上传路径 在utf8-jsp/jsp/conf ...

  2. python 学习分享-实战篇高级的ftp

    #server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...

  3. python中字符串是特殊的列表

    for x in range(20): print 'fizz'[x%3*4::]+'buzz'[x%5*4::]or x 这个是由 Jeff Atwood推广的一个编程练习叫FizzBuzz,问题如 ...

  4. vb如何将数据库中某个字段显示在一个文本框

    Dim mrc As ADODB.Recordset Private Sub cmdQuery_Click() Dim txtSQL As String Dim MsgText As String t ...

  5. 将MSHFlexGrid1中记录导出为Excel

    1.添加引用Microsoft Excel 14.0 Object Library 2.编写代码部分 Private Sub Output_Click() Dim i As Integer '定义变量 ...

  6. PHP面向对象 封装与继承

    知识点: PHP封装三个关键词: 一.public 公有的,被public修饰的属性和方法,对象可以任意访问和调用 二.private 私有的,被private修饰的属性和方法,只能在类内部的方法可以 ...

  7. PHP生成随机数函数rand(min,max)

    rand(min,max):生成min到max 的随机数,注意:包括边界rand() 返回 0 到 RAND_MAX 之间的伪随机整数.例如,想要 5 到 15(包括 5 和 15)之间的随机数,用 ...

  8. JAVA本地文本读取---解决中文乱码

    import java.io.*; public class ReadFile { public static void main(String[] args) { try { File file = ...

  9. Recompile Squid with SSL Bump

    https://docs.diladele.com/administrator_guide_4_0/system_configuration/https_filtering/recompile_squ ...

  10. Codeforces Round #324 (Div. 2) B

    B. Kolya and Tanya time limit per test 1 second memory limit per test 256 megabytes input standard i ...