当前位置:网站首页>41. The first missing positive number
41. The first missing positive number
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
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
Two 、 Problem solving
The array is treated as a hash table ( In situ hash )
take [1,n] Put the elements in i-1 The subscript , Exchange two values , Guarantee num[i] stay i-1 The subscript , If it doesn't exist , Just put it in that position [1,n] Between .
class Solution {
public int firstMissingPositive(int[] nums) {
// take [1,n] Put the elements in i-1 The subscript , Exchange two values
int length = nums.length;
for(int i = 0;i<length;i++){
// Guarantee num[i] stay i-1 The subscript , If it doesn't exist , Just put it in that position [1,n] Between
while(nums[i] > 0 && nums[i] <= length && nums[nums[i]-1] != nums[i]){
swap(nums,i,nums[i]-1);
}
}
// Traversal array
for(int i = 0;i<length;i++){
if(nums[i] != i+1){
return i+1;
}
}
return length+1;
}
public void swap(int[] nums,int left,int right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009892.html
边栏推荐
- Understanding and small examples of unity3d object pool
- Header built-in object
- Advantages and disadvantages of several note taking software
- Input file upload
- Promise (III)
- Shell-sed命令的使用
- How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
- Use of shell sed command
- Manually implement call, apply and bind functions
- Indexes and views in MySQL
猜你喜欢
If you start from zero according to the frame
常用SQL语句总结
Use of todesk remote control software
STM32 entry development board choose wildfire or punctual atom?
Detailed explanation of C webpai route
uni-app黑马优购项目学习记录(下)
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Future 用法详解
Matlab / Simulink simulation of double closed loop DC speed regulation system
Perception of linear algebra 2
随机推荐
Halo 开源项目学习(二):实体类与数据表
Indexes and views in MySQL
Low code development platform sorting
1-5 nodejs commonjs specification
Using quartz under. Net core - [1] quick start
XTask与Kotlin Coroutine的使用对比
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
C dapper basically uses addition, deletion, modification and query transactions, etc
Promise (III)
Matlab / Simulink simulation of double closed loop DC speed regulation system
flink 学习(十二)Allowed Lateness和 Side Output
【WPF绑定3】 ListView基础绑定和数据模板绑定
Deep understanding of control inversion and dependency injection
Use of Shell sort command
[WPF binding 3] listview basic binding and data template binding
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Preliminary understanding of promse
Summary of common SQL statements
Promise (II)
EF core in ASP Generate core priority database based on net entity model