当前位置:网站首页>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;
};
边栏推荐
- Double pointeur en langage C - - modèle classique
- Promise 在uniapp的简单使用
- The harm of game unpacking and the importance of resource encryption
- 深度剖析C语言指针
- [Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
- Bitwise logical operator
- LeetCode:26. 删除有序数组中的重复项
- LeetCode:39. 组合总和
- 个人电脑好用必备软件(使用过)
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
猜你喜欢
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
ROS compilation calls the third-party dynamic library (xxx.so)
Using C language to complete a simple calculator (function pointer array and callback function)
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
可变长参数
个人电脑好用必备软件(使用过)
LeetCode:124. 二叉树中的最大路径和
企微服务商平台收费接口对接教程
Navicat Premium 创建MySql 创建存储过程
C语言深度解剖——C语言关键字
随机推荐
Sublime text using ctrl+b to run another program without closing other runs
Super efficient! The secret of swagger Yapi
LeetCode:剑指 Offer 03. 数组中重复的数字
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
sublime text的编写程序时的Tab和空格缩进问题
【嵌入式】Cortex M4F DSP库
Problems encountered in connecting the database of the project and their solutions
Detailed explanation of heap sorting
【剑指offer】序列化二叉树
Revit secondary development Hof method calls transaction
Swagger setting field required is mandatory
Li Kou daily question 1 (2)
Mongodb installation and basic operation
Philosophical enlightenment from single point to distributed
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
egg. JS project deployment online server
ROS compilation calls the third-party dynamic library (xxx.so)
C language double pointer -- classic question type
LeetCode:162. 寻找峰值