当前位置:网站首页>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;
};
边栏推荐
- Trying to use is on a network resource that is unavailable
- 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
- JVM quick start
- View computer devices in LAN
- CSP first week of question brushing
- To effectively improve the quality of software products, find a third-party software evaluation organization
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- Detailed explanation of heap sorting
- 力扣每日一题(二)
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
猜你喜欢
visdom可视化实现与检查介绍
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
Delay initialization and sealing classes
Navicat premium create MySQL create stored procedure
UML图记忆技巧
marathon-envs项目环境配置(强化学习模仿参考动作)
egg. JS getting started navigation: installation, use and learning
Navicat Premium 创建MySql 创建存储过程
TP-LINK enterprise router PPTP configuration
[embedded] cortex m4f DSP Library
随机推荐
Unsupported operation exception
LeetCode:39. 组合总和
Philosophical enlightenment from single point to distributed
C語言雙指針——經典題型
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
Deep analysis of C language pointer
LeetCode:剑指 Offer 42. 连续子数组的最大和
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
The network model established by torch is displayed by torch viz
Excellent software testers have these abilities
LeetCode:41. 缺失的第一个正数
超高效!Swagger-Yapi的秘密
Revit 二次开发 HOF 方式调用transaction
Function coritization
PC easy to use essential software (used)
What are the common processes of software stress testing? Professional software test reports issued by companies to share
Swagger setting field required is mandatory
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
Problems encountered in connecting the database of the project and their solutions
opencv+dlib实现给蒙娜丽莎“配”眼镜