当前位置:网站首页>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数据库常识之储存引擎
- 【栈和队列专题】—— 滑动窗口
- 居家第二十三天的午饭
- Go language development Daily Fresh Project Day 3 Case - Press Release System II
- Analysis of the relationship between generalized Bim and CAD under the current background
- Elastic box model
- LeetCode-279-完全平方数
- pikachuxss如何获取cookie靶场,返回首页总是失败
- Rt-1052 learning notes - GPIO architecture analysis
- Linux中,MySQL的常用命令
猜你喜欢

100天拿下11K,转岗测试的超全学习指南

Latex formula

Flex layout

上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案

内网渗透之DOS命令

3-5通过XSS获取cookie以及XSS后台管理系统的使用

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

一些接地气的话儿

Linux中,MySQL的常用命令

LeetCode 116. 填充每个节点的下一个右侧节点指针
随机推荐
Cmake project under vs2019: calculating binocular parallax using elas method
How do BIM swindlers cheat? (turn)
Leetcode 74. Search two-dimensional matrix
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
Matlab analytic hierarchy process to quickly calculate the weight
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
Vscode download speed up
LeetCode 116. Populate the next right node pointer for each node
go array
Bash script learning -- for loop traversal
学会打字后的思考
內網滲透之DOS命令
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
Matlab matrix index problem
MySQL stored procedures and functions
Learn to C language fourth day
Leetcode 1351. Negative numbers in statistical ordered matrices
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
41. 缺失的第一个正数
缓存淘汰算法初步认识(LRU和LFU)