First Missing Positive
class Solution {
public int firstMissingPositive(int[] nums) {
for(int i=0;i<nums.length;i++){
int correctPos=nums[i]-1;
while(((1<=nums[i])&&(nums[i]<=nums.length))&&nums[i]!=nums[correctPos])
{
int t=nums[i];
nums[i]=nums[correctPos];
nums[correctPos]=t;
correctPos=nums[i]-1;
}
}
for(int i=0;i<nums.length;i++){
if(i+1!=nums[i]){
return i+1;
}
}
return nums.length+1;
}
}
Comments
Post a Comment