LeetCode 41. First Missing Positive(Hard)

【GAMES101】现代计算机图形学入门 资料整理 课程网址 课程主页 包括ppt 课程PPT和视频 一篇搞定!GAMES101现代计算机图形学入门(全) 作业链接地址 作业论坛 GAMES101课程环境搭建(Win10+VSCode) 计算机图形学笔记 虎书pdf 提取:n2rx 【必须】:高等数学(微积分、线性代数)、算法、数据结构 【非必须】:信号处理、数值分析、大学物理(光学) 在线公式编辑器 一定要做作业,如果个人要配置环境的话应该是配置opencv,eigen这两个核心库就行了,目前觉得最核心的一个作业是作业3,实现着色器和渲染管线,做完一遍 阅读详情

查阅更多的题解,请点击

Problem

41. First Missing Positive(Hard)

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.

Solution

题目主要抓住两点信息,即可得出解法:

  • 所求firstMissingPositive一定在闭区间[1,n+1],有该观察,可以得到O(n) time, O(n) space的解法
  • 可以用输入数组nums来保存元素的访问情况,则可以将O(n) space的方法优化到O(1) space

O(n) time, O(n) space

class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        if (nums.empty())
            return 1;
        int n = nums.size(), res = 0;
        vector<bool> visited(n + 2, false);
        for (auto num : nums)
        {
            if (num <= n && num > 0)
            {
                visited[num] = true;
            }
        }
        for (int i = 1; i <= n + 1; ++i)
        {
            if (!visited[i])
            {
                res = i;
                break;
            }
        }
        return res;
    }
};

O(n) time, O(1) space

class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        if (nums.empty())
            return 1;
        int n = nums.size();
        int res = n + 1;
        for (int i = 0; i < n; ++i)
        {
            while (nums[i] <= n && nums[i] > 0 && nums[i] != nums[nums[i] - 1])
            {
                swap(nums[i], nums[nums[i] - 1]);
            }
        }
        for (int i = 0; i < n; ++i)
        {
            if (nums[i] != i + 1)
            {
                res = i + 1;
                break;
            }
        }
        return res;
    }
};

GitHub传送门

使用PaddleOCR识别查验平台验证码 PaddleOCR识别国税查验平台验证码 阅读详情

相关推荐

ASTRA Toolbox学习笔记-Python

ASTRA Toolbox 是一个用于处理 X 射线相干散射和 X 射线传输成像的开源算法工具箱。本文档概述了该工具箱的基本概念。

hou9021的博客 8261

LeetCode C++ 41. First Missing Positive【Array】困难

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 const

memcpy0的博客 395

【通信协议-RTCM】MSM语句(1) - 多信号GNSS观测数据消息格式

注释:RTCM响应消息1020为GLONASS星历信息,暂不介绍,前公司暂未研发RTCM消息类型版本的DR/RTK模块,DR/RTK模块仅NMEA消息类型使用注释:公司使用的多信号语句类型为MSM4&MSM7,也应该是运用最广泛的语句类型,例如本人之前在公司开发的工具全是基于MSM7语句类型来进行的。

qxj2422640226的博客 2093

leetcode 150道题目的原题

1:https://oj.leetcode.com/problems/reverse-words-in-a-string/ name:Reverse Words in a String    date:2014-03-05    rate:14.0% Reverse Words in a String Total Accepted: 34174 Total Submissions: 244

qqqil的专栏 3983

深入解析Gerber RS274X文件格式:从基础语法到实战应用

本文深入解析PCB制造的核心标准Gerber RS274X文件格式,详细介绍了其基础语法、坐标系统、功能码以及嵌入式光圈定义等关键特性。通过实战案例演示了如何解析和手写Gerber指令,并总结了常见错误与调试技巧,帮助PCB工程师掌握这一与制造工厂沟通的“通用语言”,确保设计准确无误地转化为实体电路板。

weixin_29252171的博客 95

行列式详解:从定义到应用

本文系统介绍了行列式的核心概念和应用。首先从二阶、三阶行列式入手,逐步扩展到n阶行列式的定义,讲解了余子式和代数余子式的概念。其次,详细阐述了行列式的重要性质,包括转置不变性、行列交换变号等基本性质以及乘积性质等推论。在计算方法方面,介绍了按行展开法、化上三角矩阵法等实用技巧,并列举了特殊行列式的计算案例。最后重点讨论了行列式在克拉默法则、矩阵可逆性判断、几何体积计算及线性相关性判断等领域的应用。文章强调理解行列式的几何意义和灵活运用性质的重要性,并给出了学习建议和常见误区提示。行列式作为连接代数与几何的桥

aichitang2024的博客 3036

leetcode hard模式专杀leetcode 41 First Missing Positive

这题要求在O(n)时间复杂度,O(1)空间复杂度完成,摆明了只能扫描一次,并且几乎不能用额外的存储结构。 那么我的第一反应思路是用比特位,初始化一个全0的比特位图,然后逐个扫描数组元素,假设数组长度为x,则一次扫描这x元素,如果是碰到正数,则在该数对应的比特位放置1,例如如果是5,则在第5位置1。 我是用的Java,里面有个很好的数据结构BitSet很快就写出来了这个逻辑,并且经过测试也...

xunileida的专栏 236

《通信工程施工与管理》

制作馈线接头必须使用专用的工具,专用工具包括:馈线切割刀、馈线安全刀、小金属丝毛刷、固定尺寸或大号活动扳手、小号扁锉刀、细齿小手锯、接头扩孔器、3M防水胶或防水接头盒;如图所示,详情如表所示。

2302_78710464的博客 1653

【LeetCode】 41. First Missing Positive 缺失的第一个正数(Hard)(JAVA)

【LeetCode】 41. First Missing Positive 缺失的第一个正数(Hard)(JAVA) 题目地址: https://leetcode.com/problems/first-missing-positive/ 题目描述: Given an unsorted integer array, find the smallest missing positive integer...

qq_16927853的博客 190

【BLE】HID设备开发实战:从自拍杆到复合设备的蓝牙控制方案

本文深入解析了BLE HID(低功耗蓝牙人机接口设备)的开发实战,从蓝牙自拍杆的工作原理切入,详细阐述了HID Report Map的设计与实现。通过拆解自拍杆、键盘、鼠标等案例,指导开发者如何利用BLE的低功耗与即插即用特性,构建从单一功能到键盘鼠标复合设备的完整蓝牙控制方案。

weixin_29159613的博客 466

漫画安全HIDS、EDR、NDR、XDR

漫画安全HIDS、EDR、NDR、XDR

墨痕诉清风的博客 3446

[LeetCode][Array] (hard) 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 Soluti...

Renoler的博客 98

乐企增值税及附加税费(一般纳税人适用)申报表填报能力说明文档V3.009

申报表填报,是指企业在自有业务系统中将算税结果数据按预填规则要求加工汇总后,进行申报表填报的过程。申报表填报前对纳税申报所需的前提条件进行校验。校验通过后,应用前序算税结果数据及预填规则进行纳税申报表预填,生成申报表数据。申报表数据通过申报业务规则校验后,经纳税人确认即完成申报表填报业务。

记录点滴 346

【leetcode】41. First Missing Positive 【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

qq_30918847的博客 220

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

leetcode hard模式专杀之41 First Missing Positive

这题看下来第一灵感就是用位运算,因为要用 O(n) time and uses constant space.,于是很快地写了一版,果不其然,用位运算一定要考虑溢出的问题啊,第一版草草这么写果然就溢出了,因为把1 左移整数位,java这边移动个32位基本就到顶了,就算用long,移个64位差不多也到顶了,于是想到一个解决方案即使用多个整数,一个整数就算能表示k个位置,两个整数不就可以表示2k个

xunileida的专栏 307

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode41 - First Missing Positive - hard

Given an unsorted integer array, find the smallest missing positive integer.Example 1:Input: [1,2,0]Output: 3Example 2:Input: [3,4,-1,1]Output: 2Example 3:Input: [7,8,9,11,12]Output: 1Note:Your algori...

weixin_30432179的博客 114

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode Top Interview Questions 41. First Missing Positive (Java版; Hard)

welcome to my blog LeetCode Top Interview Questions 41. First Missing Positive (Java版; Hard) 题目描述 Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,...

littlehaes的博客 153

【LeetCode】41. First Missing Positive(Hard)解题报告

【LeetCode】41. First Missing Positive(Hard)解题报告 题目地址:https://leetcode.com/problems/first-missing-positive/description/ 题目描述:   Given an unsorted integer array, find the first missing positive int...

郝春雨的博客 193

【leetcode】41.(Hard)First Missing Positive

题目链接 解题思路: 归并排序——剔重——找到最小非负数——折半查找 这道题就是需要考虑的边界比较多 另外就是对重复数字的处理 提交代码: class Solution { public int firstMissingPositive(int[] nums) { if(nums.length==0) return 1; if(nums.length==1) ...

AXIMI的博客 217

[leetcode]41. First Missing Positive,C++/python实现,hard难度

[leetcode]41. First Missing Positive,C++/python实现,hard难度

zl87758539的专栏 994

LeetCode算法题之41. First Missing Positive(hard)【Python3题解】

题目描述: 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 ...

DaMoWangZQ的博客 239

Leetcode 41. First Missing Positive

Leetcode 41. First Missing Positive 算法: Check if 1 is present in the array. If not, you’re done and 1 is the answer. If nums = [1], the answer is 2. Replace negative numbers, zeros, and numbers larger than n by 1s. 检查1是否存在,不存在返回1。如果1存在,且数组只有一个元素,则返回2。把所有0

weixin_43876597的博客 295

LeetCode No.41 First Missing Positive 题解

LeetCode No.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. You

Guo15331092的博客 502

Leetcode 41 First Missing Positive

Leetcode 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 Follow up: Y

weixin_43796689的博客 209

LeetCode41——First Missing Positive

LeetCode41——First Missing Positive 题意: 在序列中找到第一个没有出现的正整数。 我的方法比较笨,后来提交过后通过的确时间效率非常低。 首先,排序、去重。 然后对这个有序序列进行迭代: 找到正整数开始的索引i,从这个索引开始,该序列的数后面应该是按 1 2 3 4 5 6 7...这样的顺序来排列。 所以对于从索引i开始的每一个元素nums[

上善若水 607

LeetCode刷题:41. First Missing Positive

LeetCode刷题:41. First Missing Positive 原题链接:https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 248
上一篇: LeetCode 140. Word Break II(Hard)
下一篇: LeetCode 395. Longest Substring with At Least K Repeating Characters(Medium)题解
彼得.攀
博客等级 码龄11年 11粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值