-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstMissingPositive.java
47 lines (41 loc) · 1.08 KB
/
FirstMissingPositive.java
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
39
40
41
42
43
44
45
46
47
/*Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
*/
// unfortunately this solution is not O(n)
class Solution {
public int firstMissingPositive(int[] nums) {
if(nums == null || nums.length == 0) return 1;
Arrays.sort(nums);
if (nums[nums.length-1] < 1)
return 1;
int tracker = 1;
boolean hasOne = false;
for(int i : nums) {
if (i==1)
hasOne = true;
if (i>1) {
if (i!=tracker) {
if (i==tracker+1 && hasOne) {
tracker++;
} else {
if (hasOne)
return tracker+1;
else
return tracker;
}
}
}
}
return nums[nums.length-1]+1;
}
}