当前位置:网站首页>41. 缺失的第一个正数
41. 缺失的第一个正数
2022-04-23 20:40:00 【秀秀的奇妙旅行】



哈希表法(空间n)

新建数组法(空间时间n)

class Solution {
public int firstMissingPositive(int[] nums) {
int len=nums.length;
//先构造一个临时数组 tmp
int[] temp=new int[len];
int j;
for(int i=0;i<len;i++){
if(nums[i]>0&&nums[i]<=len){
//把 A[i] 复制到 tmp[A[i]-1] 的位置
temp[nums[i]-1]=nums[i];
}
}
//然后从位置 0 开始检查 tmp,如果发现该位置的值和索引号不匹配,就说明找到了缺失的数了
for(int i=0;i<len;i++){
if(temp[i]!=i+1){
return i+1;
}
}
return len+1;
}
}
交换位置法(空间1 时间n)

class Solution {
public int firstMissingPositive(int[] nums) {
int len=nums.length;
for(int i=0;i<len;i++){
//nums[i] =4 跟 num[3]换
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的是i+1 如果nums[i=1] 3 ; 则应该返回3
return i+1;
}
}
// 所有的都符合 那么最小的没有出现的就是len+1 {1,,2,3}返回4
return len+1;
}
}
改变数组法

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;
}
}
版权声明
本文为[秀秀的奇妙旅行]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yunxiu988622/article/details/124290982
边栏推荐
- Syntaxerror: unexpected token r in JSON at position 0
- I JS deep copy and shallow copy
- Preliminary understanding of cache elimination algorithm (LRU and LFU)
- go-zero框架数据库方面避坑指南
- Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
- 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
- Tensorflow 2 basic operation dictionary
- Commit and ROLLBACK in DCL of 16mysql
- SQL gets the latest record of the data table
- Matlab matrix index problem
猜你喜欢

A login and exit component based on token

BMP JPEG picture to vector image contourtrace

Unity Odin ProgressBar add value column

LeetCode 542、01 矩阵

【栈和队列专题】—— 滑动窗口

Matlab matrix index problem

Solution: NPM err! code ELIFECYCLE npm ERR! errno 1

Vulnhub DC: 1 penetration notes

UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value

Automatically fill in body temperature and win10 task plan
随机推荐
Es index (document name) fuzzy query method (database name fuzzy query method)
Async function ------ ES6
go-zero框架数据库方面避坑指南
缓存淘汰算法初步认识(LRU和LFU)
Leetcode 994, rotten orange
C# 知识
Solve the Chinese garbled code of URL in JS - decoding
一. js的深拷贝和浅拷贝
A useless confession artifact
Thirty What are VM and VC?
16MySQL之DCL 中 COMMIT和ROllBACK
16MySQL之DCL 中 COMMIT和ROllBACK
Modeling based on catiav6
Fastdfs思维导图
Go language development Daily Fresh Project Day 3 Case - Press Release System II
How many hacking methods do you know?
Leetcode 20. Valid parentheses
Matlab: psychtoolbox installation
深入探究ASP.NET Core读取Request.Body的正确方式
內網滲透之DOS命令