Last active
August 29, 2015 14:22
-
-
Save cangoal/7f5480e11a9b27a643fa to your computer and use it in GitHub Desktop.
LeetCode - First Missing Positive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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