leetcode第40题--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,5,4]那就是缺3.

思路:利用counting sort的想法。遍历一次,如果当前的数超出了 1 到 n-1的范围,跳过。如果是1到n-1但是,A[i]不等于i+1,并且在 A[i] - 1 的位置的数不满足 A[A[i]-1]+1的要求,那就将两个位置的数置换。同时,还要在当前位置判断换过来的数是否满足上述要求。这个哥们的图很好理解。

class Solution {
public:
    void swap40(int A[], int i, int j) // 其实std有自带的函数swap(A[i], A[j])
    {
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
        return;
    }
    int firstMissingPositive(int A[], int n)
    {
        if (n <= 0 )
            return 1;
        for (int i = 0; i < n; ++i)
        {
            if (A[i] > 0 && (A[i] - 1 < n) && A[i] != i + 1 && A[i] != A[A[i] - 1])
                {swap(A, i, A[i] - 1);i--;} // i--是为了和之后的++i持平,因为交换的条件满足的话,交换过来的数还要再判断
        }
        for (int i = 0; i < n; ++i)
        {
            if (A[i] != i + 1)
                return i + 1;
        }
        return n + 1;
    }
};
时间: 2024-08-24 21:26:03

leetcode第40题--First Missing Positive的相关文章

leetcode第一刷_ First Missing Positive

未排序数组,O(N)时间,常数空间,这道题让我非常清晰的感觉到算法的魅力. 先想一下如果允许用额外空间的话,我们会怎么做,对,我们会建立一个hash表,然后从头到尾的扫描数组,等等,怎么映射呢?有n个数,要找第一个消失的正正整数,那么这个消失的正整数的取值范围是什么呢?[1, n+1],之所以包含n+1是因为如果这n数正好是连续的前n个自然数.那我们就知道了,开一个长为n的哈希表,如果当前扫到得数是[1,n]之间的话,就放到数值减1的位置上,如果不是的话就跳过,然后从头扫一遍这个哈希表,第一个没

LeetCode(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. 分析 题目给定一个无序序列,要求返回第一个丢失的正整数. 限制时间复杂度为O(n),空间消耗为

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)的时间复杂度以及常数级

【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 - 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][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. 算法分析: 将1 ~ a.length的数字放到与下标相应

leetcode First Missing Positive hashset简单应用

1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 HashSet<Integer> hash=new HashSet<Integer>(); 4 int count=0; 5 int sum=0; 6 7 for(int i:A) 8 { 9 if(i>0) 10 { 11 hash.add(i); 12 } 13 } 14 15 int beg=1; 16 while(hash.

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. 二. 题目分析 该题的大意是给定一个未排序的数组,该数组可能包含零或正负数,要求找出