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)的空间,则非常简单,首先遍历完数组,建立hash表,第二轮遍历直接找出第一个缺失的正数

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        assert(A != NULL && n >= 0);
        unordered_set<int> numSet;
        for (int i = 0; i < n; ++i) {
            numSet.insert(A[i]);
        }

        for (int i = 1; ; ++i) {
            auto iter = numSet.find(i);
            if (iter == numSet.end()) {
                return i;
            }
        }
    }
};

空间复杂度O(1)的解法:

桶排序的思想,最基本的思想就是 每个元素都归位到固定编号的桶内

这里正常情况下,应该是 1放在index = 0位置上,2放在index = 1的位置上,类似的,我们可以理解为 A[i]应该归位到编号为A[i]-1的桶内

当A[i] = i + 1时,元素已经位于正确的桶内了,直接跳过

当A[i] != i + 1时,我们不断的 交换 A[i] 与 A[A[i] - 1] 这两个值,直到发生以下情况:

a. A[i] = i + 1,循环结束

b. 因为与 A[A[i] - 1]做交换,所以 A[i] - 1作为下标必须符合范围,即 1 <= A[i] <= n,否则直接break

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        bucketSort(A, n);
        for (int i = 0; i < n; ++i) {
            if (A[i] != i + 1) {
                return i + 1;
            }
        }
        return n + 1;

    }

    void bucketSort(int A[], int n) {
        assert(A != NULL && n >= 0);
        for (int i = 0; i < n; ++i) {
            while (A[i] != i + 1) {
                if (A[i] <= 0 || A[i] > n || A[i] == A[A[i] - 1]) {
                    break;
                }
                std::swap(A[i], A[A[i] - 1]);
            }
        }
    }
};

Leetcode:First Missing Positive 第一个缺失的正数 桶排序

时间: 2024-08-24 15:12:40

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. 这道题让我们找缺失的首个正数,由于限定了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 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 [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

MissingNumber缺失的数字,FirstMissingPositive第一个缺失的正数

MissingNumber问题描述:给定一个数组,数组数字范围是0-n,找到缺失的数字.例如nums={0,1,3},return2. 算法分析:第一种方法,对数组进行排序,然后找到和下标不一致的数字,下标即为缺失的数字.第二种方法,对于这样的数组,如果没有缺失数字,即使没有排序,下标-数字的差相加为0.假设缺失的数字在nums.length位置,那么sum+nums.length - x = 0; 1 public int missingNumber(int[] nums) { 2 Array

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