当前位置:网站首页>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
边栏推荐
- "Meta function" of tidb 6.0: what is placement rules in SQL?
- Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
- LeetCode 709、转换成小写字母
- [SQL] string series 2: split a string into multiple lines according to specific characters
- 学会打字后的思考
- Selenium displays webdriverwait
- Come in and teach you how to solve the problem of port occupation
- bounding box iou
- Introduction to standardization, regularization and normalization
- MySQL进阶之常用函数
猜你喜欢

中创存储|想要一个好用的分布式存储云盘,到底该怎么选

"Meta function" of tidb 6.0: what is placement rules in SQL?

Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified

MySQL基础合集

Flex layout

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

GO語言開發天天生鮮項目第三天 案例-新聞發布系統二

JS arrow function user and processing method of converting arrow function into ordinary function

上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案

Leetcode 74. Search two-dimensional matrix
随机推荐
2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
Rt-1052 learning notes - GPIO architecture analysis
【栈和队列专题】—— 滑动窗口
LeetCode 1346、检查整数及其两倍数是否存在
Leetcode 1346. Check whether integers and their multiples exist
Introduction to intrusion detection data set
一. js的深拷贝和浅拷贝
On IRP from the perspective of source code
SQL gets the latest record of the data table
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
41. 缺失的第一个正数
Awk example skills
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
常用60类图表使用场景、制作工具推荐
The ODB model calculates the data and outputs it to excel
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Leetcode 74. Search two-dimensional matrix
Commande dos pour la pénétration de l'Intranet
LeetCode 20、有效的括号