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.

如果数组中的数是按照数该在的位置摆放(数i摆放在数组i的位置),那么很容易就能获得第一个缺失的正数。

所以我们先调整数组数的位置,令下标为i的位置存放数i。

再遍历一遍数组,如果nums[i]!=i,说明该位置的数缺失。

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        int numsSize = nums.size();
        if(numsSize==0 ){
            return 1;
        }
        for(int i=0;i<numsSize;i++){
            while(nums[i] != i){//如果下标i的位置存放的数不是i,则把nums[i]放在下标为nums[i]的位置
                if(nums[i]>=numsSize || nums[i]<=0 || nums[i] == nums[nums[i]]){//如果是负数或者大于numsSize的数,则位置不变
                    break;
                }
                swap(nums[i],nums[nums[i]]);
            }
        }
        for(int i=1;i<numsSize;i++){
            if(i!=nums[i]){
                return i;
            }
        }
        return nums[0]==numsSize? numsSize+1 : numsSize;
    }
};
时间: 2024-11-05 23:22:46

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

41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法

41. 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,4,5,

41. First Missing Positive(C++)

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. Solution: class Solution { public: int firstMissingP

LeetCode41 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.(Hard) 分析: 如果题目要求是O(n^2)时间复杂度,O(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

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

在leetCode上做的第一个难度是hard的题,题目如下: 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. 关键是要实现0(N)的时间复杂度以及常数级

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][Python]41: First Missing Positive

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 41: First Missing Positivehttps://oj.leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,a

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. SOLUTION 1: 使用类似桶排序的方法: 将值放在它