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. 【转载】Linux下安装LoadRunner LoadGenerator

    原文地址:[转载]Linux下安装LoadRunner LoadGenerator作者:邱建忠tester LR的负载机安装在linux的理由: 1.windows xp,双核+4G内存,基本上每个v ...

  2. python 学习分享-socket编程

    socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思. 通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟 ...

  3. ssh-centos74.sh

    #!/bin/bash sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config sed -i 's/#Us ...

  4. Python列表及元组操作

    #列表(一组有序数据的组合就是列表) #创建列表 #空列表 var = list()#var = [] print(var,type(var)) #具有多个元素的列表 var = ['风','水',' ...

  5. JAVA判断一个字符串里面有没有汉字

    private static boolean checkIfExistChineseCharacter(String s) { return !(s.length() == s.getBytes(). ...

  6. 【计算机网络基础】用 telnet 检查 VPS 的某个端口是否处于监听状态

    命令 telnet x.x.x.x p 其中 x.x.x.x 代表 VPS 的 IP 地址,p 代表要检查的端口号. 起因是我的 ss 不好使了.IP 没被封,查了一下,可能是 ssserver 的端 ...

  7. Codeforces 359D Pair of Numbers | 二分+ST表+gcd

    题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...

  8. Codeforces Round #281 (Div. 2) B 模拟

    B. Vasya and Wrestling time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. DP———3.最长上升子序列的和

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...

  10. Pandas之DataFrame——Part 2

    ''' [课程2.] 时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() ...