[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)的时间,所以一般的排序方法都不能用,最开始我没有看到还限制了空间复杂度,所以想到了用哈希表来解,这个思路很简单,第一遍遍历数组把所有的数都存入哈希表中,并且找出数组的最大值,下次循环从1开始递增找数字,哪个数字找不到就返回哪个数字,如果一直找到了最大的数字,则返回最大值+1,代码如下:

// NOT constant space
class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        if (n <= 0) return 1;
        unordered_map<int, int> m;
        int mx = A[0];
        for (int i = 0; i < n; ++i) {
            if (A[i] > 0) {
                m[A[i]] = 1;
                mx = max(mx, A[i]);
            }
        }
        for (int i = 1; i <= mx; ++i) {
            if (m.find(i) == m.end()) return i;
        }
        return mx + 1;
    }
};

但是上面的解法不是O(1)的时间复杂度,所以我们需要另想一种解法,既然不能建立新的数组,那么我们只能覆盖原有数组,我们的思路是把1放在数组第一个位置A[0],2放在第二个位置A[1],即需要把A[i]放在A[A[i] - 1]上,那么我们遍历整个数组,如果A[i] != i + 1, 而A[i]为整数且不大于n,另外A[i]不等于A[A[i] - 1]的话,我们将两者位置调换,如果不满足上述条件直接跳过,最后我们再遍历一遍数组,如果对应位置上的数不正确则返回正确的数,代码如下:

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        int i = 0;
        while (i < n) {
            if (A[i] != i + 1 && A[i] > 0 && A[i] <= n && A[i] != A[A[i] - 1]) {
                swap(A[i], A[A[i] - 1]);
            } else {
                ++ i;
            }
        }
        for (i = 0; i < n; ++i) {
            if (A[i] != i + 1) return i + 1;
        }
        return n + 1;
    }
};
时间: 2024-08-01 22:49:45

[LeetCode] First Missing Positive 首个缺失的正数的相关文章

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. 题解分析: 如果是T(n)的时间 T(n)的空间,则

LeetCode: First Missing Positive [040]

[题目] 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. 算法分析: 将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 2012-03-08]

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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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. 方法: 既然是正整数,那么可以考虑将正整数跟下标对应起来存放.正整数不包括0,则下标0存放1

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

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)时间内