41. First Missing Positive

本文介绍了一种在未排序整数数组中寻找最小缺失正整数的算法,提出了两种解决方案,一种使用额外的布尔数组,另一种通过原地修改数组元素实现。重点介绍了第二种方法的实现细节和复杂度分析。

41. First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

Input: [1,2,0]
Output: 3

Example 2:

Input: [3,4,-1,1]
Output: 2

Example 3:

Input: [7,8,9,11,12]
Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

方法1:

Complexity

Time complexity: O(n)
Space complexity: O(n), although INT_MAX as the max, which could mean O(1)

思路:

创建一个以nums.size() 为长度的bool数组, 如果该数字出现,mark为true,最后遍历以便找到第一个为false的正整数

方法2:

basketking: https://www.youtube.com/watch?v=8DqewGsVNkI

思路:

首先注意到,我们可以忽略input中所有的0,负数和大于等于n的数。0和负数不影响结果,而我们的结果最大可能值是n。

对每一个遇到的正整数,将其所在的数字标记为负数,这样我们就把出现与否换成了符号的形式储存而不用覆盖原位数字。第二遍遍历找到第一个没有被反转过的数字即可。

易错点

  1. 0 也需要 变为max
  2. 这个答案还没过
class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        if (nums.size() == 0) return 1;
        for (int i = 0; i < nums.size(); i++){
            if (nums[i] <= 0){
                nums[i] = INT_MAX;
            }
        }
        
        for (int i = 0; i <  nums.size() ; i++){
            int num = abs(nums[i]);
            if (num < nums.size()){
                nums[num - 1] = - abs(nums[num - 1]);
            }  
        }
        
        for (int i = 0; i < nums.size(); i++){
            if (nums[i] > 0 && nums[i] < nums.size()){
                return i + 1;
            }
        }
        return nums.size();
        }
};

方法2:

discussion: https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c%2B%2B-solution-O(1)-space-and-O(n)-time
思路:

Put each number in its right place.

For example:

When we find 5, then swap it with A[4].

At last, the first place where its number is not right, return the place + 1.

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        //if (nums.size() == 0) return 1;
        int n = nums.size();
        for (int i = 0; i < n; i ++){
            while (nums[i] <=n && nums[i] > 0 && nums[nums[i] - 1] != nums[i]){
                swap(nums[i], nums[nums[i] - 1]);
            }
        }
        for (int i = 0; i< n; i++){
            if (nums[i] != i + 1){
                return i + 1;
            }
        }
        return n + 1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值