LeetCode: First Missing Positive

LeetCode: First Missing Positive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

import static org.junit.Assert.assertSame;
/**
* Description: Given an unsorted integer array, find the first missing positive integer.
* <p/>
* For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.
* <p/>
* Your algorithm should run in O(n) time and uses constant space.
*
* @author hzhou
*/
public class FirstMissingPositive {
public int firstMissingPositive(int[] nums) {
if (nums == null || nums.length < 1) {
return 1;
}
for (int i = 0; i < nums.length; ) {
int crt = nums[i];
if (crt > 0 && crt <= nums.length && i + 1 != crt && nums[i] != nums[crt - 1]) {
nums[i] = nums[crt - 1];
nums[crt - 1] = crt;
} else {
i++;
}
}
for (int i = 0; i < nums.length; i++) {
if (i + 1 != nums[i]) {
return i + 1;
}
}
return nums.length + 1;
}
@Test
public void test() {
int[] nums = new int[]{1, 1};
assertSame(firstMissingPositive(nums), 2);
}
}