当前位置:网站首页>LeetCode:41. Missing first positive number
LeetCode:41. Missing first positive number
2022-07-06 08:50:00 【Bertil】
Here's an unsorted array of integers nums , Please find the smallest positive integer that doesn't appear in it .
Please implement a time complexity of O(n) And solutions that use only constant level extra space .
Example 1:
Input :nums = [1,2,0]
Output :3
Example 2:
Input :nums = [3,4,-1,1]
Output :2
Example 3:
Input :nums = [7,8,9,11,12]
Output :1
Tips :
- 1 <= nums.length <= 5 * 10^5
- -2^31 <= nums[i] <= 2^31 - 1
Their thinking
1. First of all, will nums[i] Put it in the right place , Such as nums[0] = 1,nums[1] = 2,…,nums[6] = 7
2. Then traverse the array after the exchange position , Determine whether it is the corresponding position , If it is not , Then directly return the current index value + 1; If it's all , Then return directly nums.length + 1 that will do
Code
/** * @param {number[]} nums * @return {number} */
var firstMissingPositive = function(nums) {
for(let i = 0; i < nums.length; i++) {
// loop nums, Current element in (0,nums.length] Between , also nums[nums[i]-1] != nums[i], Then switch places
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++) {
// Loop the array after swapping positions , Judge the first missing positive number
if(nums[i] != i+1) {
return i+1;
}
}
// [1,2,3]
return nums.length + 1;
};
边栏推荐
- [NVIDIA development board] FAQ (updated from time to time)
- LeetCode:836. 矩形重叠
- sublime text中conda环境中plt.show无法弹出显示图片的问题
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
- On the inverse order problem of 01 knapsack problem in one-dimensional state
- 随手记01
- 电脑F1-F12用途
- Navicat Premium 创建MySql 创建存储过程
- C語言雙指針——經典題型
- Crash problem of Chrome browser
猜你喜欢
win10系统中的截图,win+prtSc保存位置
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Detailed explanation of heap sorting
PC easy to use essential software (used)
Restful API design specification
UML图记忆技巧
Visual implementation and inspection of visdom
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
swagger设置字段required必填
随机推荐
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
LeetCode:673. 最长递增子序列的个数
[MySQL] multi table query
Indentation of tabs and spaces when writing programs for sublime text
TP-LINK enterprise router PPTP configuration
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
UML diagram memory skills
visdom可视化实现与检查介绍
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Visual implementation and inspection of visdom
LeetCode:221. 最大正方形
[NVIDIA development board] FAQ (updated from time to time)
Delay initialization and sealing classes
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
Process of obtaining the electronic version of academic qualifications of xuexin.com
Problems in loading and saving pytorch trained models
个人电脑好用必备软件(使用过)
C语言双指针——经典题型