当前位置:网站首页>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
边栏推荐
猜你喜欢
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
居家第二十三天的午饭
Leetcode 74. Search two-dimensional matrix
Unity Odin ProgressBar add value column
LeetCode 116. 填充每个节点的下一个右侧节点指针
41. 缺失的第一个正数
C migration project record: modify namespace and folder name
Three. Based on ply format point cloud voxel model JS upload interface writing
go defer
随机推荐
[PTA] l1-002 printing hourglass
Summary and effect analysis of methods for calculating binocular parallax
Recognition of high-speed road signs by Matlab using alexnet
Monte Carlo py solves the area problem! (save pupils Series)
LeetCode 116. 填充每个节点的下一个右侧节点指针
go slice
启牛学堂有用吗,推荐的证券账户是否安全
黑客的入侵方式你知道几种?
UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
MySQL基础合集
LeetCode 20、有效的括号
Go limit depth traversal of files in directory
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
go defer
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
3-5通过XSS获取cookie以及XSS后台管理系统的使用
The ODB model calculates the data and outputs it to excel
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
LeetCode 1346、检查整数及其两倍数是否存在
Vulnhub DC: 1 penetration notes