-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstMissingPositive.java
More file actions
37 lines (33 loc) · 1.02 KB
/
FirstMissingPositive.java
File metadata and controls
37 lines (33 loc) · 1.02 KB
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
/**
* Leetcode problem #41, First Missing Positive
* https://leetcode.com/problems/first-missing-positive
*/
public class FirstMissingPositive {
public static void main(String[] args) {
int[] nums = { 1, -2, 3, 14, 5, 6, 7, 18 };
System.out.println(firstMissingPositive(nums));
}
public static int firstMissingPositive(int[] nums) {
boolean contains = false;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
contains = true;
}
if (nums[i] < 1 || nums[i] > nums.length) {
nums[i] = 1;
}
}
if (!contains)
return 1;
for (int i = 0; i < nums.length; i++) {
// To avoid duplicate number updates
int val = Math.abs(nums[i]);
nums[val - 1] = -1 * Math.abs(nums[val - 1]);
}
for (int i = 1; i < nums.length; i++) {
if (nums[i] > 0)
return i + 1;
}
return nums.length + 1;
}
}