First Missing Positive 2021-05-05 08:50
public static int firstMissingPositive(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
while (nums[i] > 0 && nums[i] < nums.length && nums[nums[i]-1] != nums[i]) {
swap(nums, nums[i] - 1, i);
}
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
return i + 1;
}
}
return nums.length + 1;
}
private static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
Runtime | Memory |
---|---|
0 ms | 36.5 MB |
EOF