当前位置:网站首页>41. The first missing positive number
41. The first missing positive number
2022-04-23 20:44:00 【XiuXiu's wonderful journey】
Hash table method ( Space n)
New array method ( Space time n)
class Solution {
public int firstMissingPositive(int[] nums) {
int len=nums.length;
// First construct a temporary array tmp
int[] temp=new int[len];
int j;
for(int i=0;i<len;i++){
if(nums[i]>0&&nums[i]<=len){
// hold A[i] Copied to the tmp[A[i]-1] The location of
temp[nums[i]-1]=nums[i];
}
}
// And then from the position 0 Start checking tmp, If you find that the value of this position does not match the index number , It means that the missing number is found
for(int i=0;i<len;i++){
if(temp[i]!=i+1){
return i+1;
}
}
return len+1;
}
}
Exchange position method ( Space 1 Time n)
class Solution {
public int firstMissingPositive(int[] nums) {
int len=nums.length;
for(int i=0;i<len;i++){
//nums[i] =4 Follow num[3] in
if(nums[i] > 0 &&nums[i]<len&&nums[i]!=nums[nums[i]-1]){
int temp=nums[i];
nums[i]=nums[nums[i]-1];
nums[nums[i]-1]=temp;
}
}
for(int i=0;i<len;i++){
//nums[0] 1 ;0+1 1
if(nums[i]!=i+1){
//return Yes. i+1 If nums[i=1] 3 ; You should go back to 3
return i+1;
}
}
// All meet So the smallest thing that doesn't appear is len+1 {1,,2,3} return 4
return len+1;
}
}
Change the array method
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] <= 0) {
nums[i] = n + 1;
}
}
for (int i = 0; i < n; ++i) {
int num = Math.abs(nums[i]);
if (num <= n) {
nums[num - 1] = -Math.abs(nums[num - 1]);
}
}
for (int i = 0; i < n; ++i) {
if (nums[i] > 0) {
return i + 1;
}
}
return n + 1;
}
}
版权声明
本文为[XiuXiu's wonderful journey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232040268210.html
边栏推荐
- MySQL stored procedures and functions
- Go limit depth traversal of files in directory
- Unity asset import settings
- 100天拿下11K,转岗测试的超全学习指南
- Bracket matching -- [implementation of one-dimensional array]
- Learn to C language fourth day
- 内网渗透之DOS命令
- Leetcode 232, queue with stack
- The ODB model calculates the data and outputs it to excel
- [PTA] get rid of singles
猜你喜欢
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
How can matlab obtain the truncated image in trainingimagelabeler
100天拿下11K,转岗测试的超全学习指南
GO语言开发天天生鲜项目第三天 案例-新闻发布系统二
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
MySQL基础合集
Matlab matrix index problem
Latex formula
Leetcode 74. Search two-dimensional matrix
[stack and queue topics] - sliding window
随机推荐
LeetCode 1351、统计有序矩阵中的负数
Psychological formula for converting RGB to gray value
go struct
Create vs project with MATLAB
The problem of 1 pixel border on the mobile terminal
内网渗透之DOS命令
vulnhub DC:1渗透笔记
Scrapy教程 - (2)寫一個簡單爬蟲
go slice
Leetcode 232, queue with stack
Three. Based on ply format point cloud voxel model JS upload interface writing
Analysis of the relationship between generalized Bim and CAD under the current background
MySQL进阶之常用函数
A useless confession artifact
Solve the Chinese garbled code of URL in JS - decoding
A login and exit component based on token
LeetCode 994、腐烂的橘子
How can matlab obtain the truncated image in trainingimagelabeler
LeetCode 1337、矩阵中战斗力最弱的 K 行
MySQL进阶之数据的增删改查(DML)