当前位置:网站首页>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;
};
边栏推荐
- Computer cleaning, deleted system files
- Li Kou daily question 1 (2)
- 随手记01
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- LeetCode:26. 删除有序数组中的重复项
- Revit 二次开发 HOF 方式调用transaction
- 可变长参数
- Current situation and trend of character animation
- C語言雙指針——經典題型
- Using C language to complete a simple calculator (function pointer array and callback function)
猜你喜欢

Deep analysis of C language pointer

Problems in loading and saving pytorch trained models

swagger设置字段required必填

What is CSRF (Cross Site Request Forgery)?

Computer graduation design PHP Zhiduo online learning platform

vb. Net changes with the window, scales the size of the control and maintains its relative position

JVM quick start

UML圖記憶技巧

Double pointeur en langage C - - modèle classique

【嵌入式】使用JLINK RTT打印log
随机推荐
The mysqlbinlog command uses
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
The network model established by torch is displayed by torch viz
深度剖析C语言数据在内存中的存储
Promise 在uniapp的简单使用
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
torch建立的网络模型使用torchviz显示
Indentation of tabs and spaces when writing programs for sublime text
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Tdengine biweekly selection of community issues | phase III
Deep analysis of C language data storage in memory
Current situation and trend of character animation
LeetCode:剑指 Offer 03. 数组中重复的数字
【剑指offer】序列化二叉树
egg. JS project deployment online server
Tcp/ip protocol
Shift Operators
Pytorch view tensor memory size
LeetCode:39. 组合总和
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation