当前位置:网站首页>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
边栏推荐
- 16MySQL之DCL 中 COMMIT和ROllBACK
- How to do after winning the new debt? Is it safe to open an account online
- JS arrow function user and processing method of converting arrow function into ordinary function
- 2021-06-29 C escape character cancellation and use
- LeetCode-279-完全平方数
- 2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
- Introduction to intrusion detection data set
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- 【PTA】L1-002 打印沙漏
- I JS deep copy and shallow copy
猜你喜欢
Recommend an open source free drawing software draw IO exportable vector graph
Tensorflow 2 basic operation dictionary
Fastdfs思维导图
A useless confession artifact
JS arrow function user and processing method of converting arrow function into ordinary function
Go language development Daily Fresh Project Day 3 Case - Press Release System II
DOS command of Intranet penetration
Vulnhub DC: 1 penetration notes
RT-1052学习笔记 - GPIO架构分析
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
随机推荐
Recognition of high-speed road signs by Matlab using alexnet
MySQL stored procedures and functions
內網滲透之DOS命令
Commit and rollback in DCL of 16 MySQL
The iswow64process function determines the number of program bits
Solve the Chinese garbled code of URL in JS - decoding
Learn to C language fourth day
Go限制深度遍历目录下文件
Es index (document name) fuzzy query method (database name fuzzy query method)
Linux64Bit下安装MySQL5.6-不能修改root密码
[stack and queue topics] - sliding window
BMP JPEG 图片转换为矢量图像 ContourTrace
Leetcode 232, queue with stack
SQL: query duplicate data and delete duplicate data
Scripy tutorial - (2) write a simple crawler
【PTA】L1-006 连续因子
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
内网渗透之DOS命令
On BIM data redundancy theory
LeetCode 1351、统计有序矩阵中的负数