leetcode笔记:First Missing Positive

一. 题目描述

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, 2, 0],由于第一个未出现的正整数是3,则返回3[3, 4, -1, 1]第一个未出现的正整数是2,则返回2。算法要求在O(n)的时间复杂度及常数空间复杂度内完成。

一种方法是,先把数组里每个正整数从i位放到第i-1位上,这样就形成了有序的序列,然后检查每一下标index与当前元素值,就能知道当前下标所对应的正整数是否缺失,若缺失则返回下标index + 1即可。

三. 示例代码

#include <iostream>
#include <vector>

using namespace std;

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

        for (int i = 0; i < n; ++i)
        {
            if (i + 1 != nums[i])
                return i + 1;
        }

        return n + 1;
    }
};
时间: 2024-10-18 15:50:02

leetcode笔记:First Missing Positive的相关文章

【leetcode】 First Missing Positive

[LeetCode]First Missing Positive 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. 找到第一个没有出现的正整数 思路:

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

leetcode - 41. First Missing Positive - Hard descrition 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 s

【LeetCode】First Missing Positive 解题报告

[题目] 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. [解析] 题意:给定一个数组,找出第一个缺失的正数,要求时间复杂度为O(n),空间复杂度为

[LeetCode][JavaScript]First Missing Positive

First Missing Positive 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. https://leetcode.com/problems

LeetCode题解-----First Missing Positive

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. 分析: 因为数组的大小为n,因此那个缺失的整数只可能的范围[1,n+1] 方法一:需要O(n)的空间,设

LeetCode - 41. First Missing Positive

41. First Missing Positive Problem's Link ---------------------------------------------------------------------------- Mean: 给你一组整数,找出第一个空缺的正整数. 要求:时间O(n),空间O(n). analyse: 这题时间O(n)想了半天没想到,用O(n*logn)过的. 然后看了discuss,想法非常巧妙,自愧不如. Time complexity: O(N) v

Java for LeetCode 041 First Missing Positive

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. 解题思路一: 刚看到题目的时候感觉无从下手,后来仔细理解题意,需要找到first missing p

LeetCode 041 First Missing Positive

题目要求:First Missing Positive 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. 代码如下: class Solution { p

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

First Missing Positive 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. 思路:这个题刚開始是没有思路的,难就难在O(n)时间内

【leetcode】First Missing Positive

First Missing Positive 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. 通过swap操作,把各个元素swap到相应的位置上,然后再