```
class Solution {
public int firstMissingPositive(int[] nums) {
int j=0;
while(j<nums.length){
if(nums[j]<=0||nums[j]>=nums.length||nums[nums[j]-1]==nums[j]) j++;
else {
int key = nums[j];
nums[j] = nums[key-1];
nums[key-1] = key;
}
}
int i=1;
while(i<nums.length+1&&nums[i-1]==i) i++;
return i;
}
}
```