Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active August 29, 2015 14:22
Show Gist options
  • Save cangoal/7f5480e11a9b27a643fa to your computer and use it in GitHub Desktop.
Save cangoal/7f5480e11a9b27a643fa to your computer and use it in GitHub Desktop.
LeetCode - First Missing Positive
public int firstMissingPositive(int[] nums) {
if(nums == null || nums.length==0) return 1;
for(int i=0; i<nums.length; i++){
while(nums[i]>0 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1]){
int temp = nums[nums[i] -1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
for(int i=0; i<nums.length; i++){
if(nums[i] != i+1){
return i+1;
}
}
return nums.length+1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment