当前位置:网站首页>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;
};
边栏推荐
- Tdengine biweekly selection of community issues | phase III
- C語言雙指針——經典題型
- Trying to use is on a network resource that is unavailable
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- JS pure function
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- hutool优雅解析URL链接并获取参数
- 个人电脑好用必备软件(使用过)
- LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
- Revit secondary development Hof method calls transaction
猜你喜欢

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台

软件卸载时遇到trying to use is on a network resource that is unavailable

Esp8266-rtos IOT development

Indentation of tabs and spaces when writing programs for sublime text

Mobile phones and computers on the same LAN access each other, IIS settings

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

UML diagram memory skills

View computer devices in LAN

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF

【ROS】usb_ Cam camera calibration
随机推荐
PC easy to use essential software (used)
Deep analysis of C language data storage in memory
电脑F1-F12用途
Mobile phones and computers on the same LAN access each other, IIS settings
POI add write excel file
角色动画(Character Animation)的现状与趋势
pytorch查看张量占用内存大小
C语言双指针——经典题型
Purpose of computer F1-F12
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Simple use of promise in uniapp
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
egg. JS directory structure
Problems in loading and saving pytorch trained models
JS native implementation shuttle box
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
【ROS】usb_ Cam camera calibration
【剑指offer】序列化二叉树
深度剖析C语言指针
[embedded] print log using JLINK RTT