Leetcode 41. 缺失的第一个正数

该篇文章介绍了如何在给定的未排序整数数组中找到第一个未出现的最小正整数,使用了原地修改数组的技巧,通过标记和遍历实现,保证了线性时间复杂度和常量级空间复杂度。
摘要由CSDN通过智能技术生成

原题链接:Leetcode 41. First Missing Positive

Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

Example 1:

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

Explanation: The numbers in the range [1,2] are all in the array.

Example 2:

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

Explanation: 1 is in the array but 2 is missing.

Example 3:

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

Explanation: The smallest positive integer 1 is missing.

Constraints:

  • 1 <= nums.length <= 105
    -231 <= nums[i] <= 231 - 1

题目大意:

有一个长度为n的整数数组nums,要求返回没有在nums中出现的最小正数

思路:原地修改数组

首先想到的暴力做法就是哈希表,但是题目中要求 O(1) 的空间复杂度,那么考虑原地修改数组。原地修改数组的思想就是通过下标做标记。如果元素t出现过,就在对应下标t-1上做标记。

nums中的数可以分为:

  • 负数和0,将他们映射为n+1
  • 正数,将他们映射为相反数

那么打完标记后再遍历一次,遇到的第一个正数的下标就是所要答案,如果遍历完就返回n+1

C++代码:

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        int n = nums.size();
        // 将非正数都变为n+1
        for(int i = 0; i < n; i++ ){
            if(nums[i] <= 0)
                nums[i] = n+1;
        }

        // 对于小于n的元素, 存在就将其变为负数
        // 由于可能多次出现, 已经变为负数, 需要取绝对值进行处理
        for (int i = 0; i < n; ++i) {
            if (abs(nums[i]) <= n) {
                nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]);
            }
        }

        for(int i = 0; i < n; i++ ){
            if(nums[i] > 0)
                return i + 1;
        }
        return n + 1;
    }
};

复杂度分析:

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值