class Solution {
public int firstMissingPositive(int[] nums) {
int i=0;
while(i<nums.length){
if(nums[i]<=0||nums[i]>nums.length||nums[i]==i+1||nums[nums[i]-1]==nums[i])i++;//超出坐标范围,正确摆放,重复值的处理
else swap(nums,nums[i]-1,i);
}
i=0;
while(i<nums.length&&nums[i]==i+1) i++;
return i+1;
}
public void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
LeetCode 41. First Missing Positive
最新推荐文章于 2024-01-08 09:04:58 发布