当前位置:网站首页>LeetCode:41. 缺失的第一个正数
LeetCode:41. 缺失的第一个正数
2022-07-06 08:44:00 【Bertil】
给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。
请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。
示例 1:
输入:nums = [1,2,0]
输出:3
示例 2:
输入:nums = [3,4,-1,1]
输出:2
示例 3:
输入:nums = [7,8,9,11,12]
输出:1
提示:
- 1 <= nums.length <= 5 * 10^5
- -2^31 <= nums[i] <= 2^31 - 1
解题思路
1.首先将nums[i]放到对应的位置,如nums[0] = 1,nums[1] = 2,…,nums[6] = 7
2.然后遍历交换位置之后的数组,判断是否是对应位置,若不是,则直接返回当前索引值 + 1;若全都是,则直接返回nums.length + 1即可
代码
/** * @param {number[]} nums * @return {number} */
var firstMissingPositive = function(nums) {
for(let i = 0; i < nums.length; i++) {
// 循环nums,当前元素在(0,nums.length]之间,并且nums[nums[i]-1] != nums[i],则交换位置
while(nums[i] > 0 && nums[i] <= nums.length && nums[nums[i]-1] != nums[i]) {
const temp = nums[nums[i]-1];
nums[nums[i]-1] = nums[i];
nums[i] = temp;
}
}
for(let i = 0; i < nums.length; i++) {
// 循环交换位置之后的数组,判断第一个缺失的正数
if(nums[i] != i+1) {
return i+1;
}
}
// [1,2,3]
return nums.length + 1;
};
边栏推荐
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- ROS compilation calls the third-party dynamic library (xxx.so)
- 个人电脑好用必备软件(使用过)
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- JS inheritance method
- Double pointeur en langage C - - modèle classique
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- [cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
- 查看局域网中电脑设备
- logback1.3. X configuration details and Practice
猜你喜欢

Bottom up - physical layer

win10系统中的截图,win+prtSc保存位置

生成器参数传入参数

Unsupported operation exception

sublime text中conda环境中plt.show无法弹出显示图片的问题

Roguelike游戏成破解重灾区,如何破局?

Charging interface docking tutorial of enterprise and micro service provider platform

查看局域网中电脑设备

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
随机推荐
Cisp-pte practice explanation
ROS编译 调用第三方动态库(xxx.so)
Esp8266-rtos IOT development
China's high purity aluminum target market status and investment forecast report (2022 Edition)
2022.02.13 - 238. Maximum number of "balloons"
JS native implementation shuttle box
Research and investment forecast report of citronellol industry in China (2022 Edition)
Colorlog combined with logging to print colored logs
Sublime text using ctrl+b to run another program without closing other runs
Double pointeur en langage C - - modèle classique
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
Bottom up - physical layer
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
Shift Operators
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
marathon-envs项目环境配置(强化学习模仿参考动作)
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
深度剖析C语言指针
[embedded] print log using JLINK RTT
Navicat premium create MySQL create stored procedure