-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (28 loc) · 736 Bytes
/
Solution.java
File metadata and controls
31 lines (28 loc) · 736 Bytes
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
package leetCode_41;
/**
* @author dimdark
*/
public class Solution {
public int firstMissingPositive(int[] nums) {
if (nums == null || nums.length == 0) {
return 1;
}
for (int i = 0; i < nums.length; ++i) {
if (nums[i] <= 0) {
nums[i] = Integer.MAX_VALUE;
}
}
for (int i = 0; i < nums.length; ++i) {
int num = Math.abs(nums[i]);
if (num <= nums.length) {
nums[num - 1] = -Math.abs(nums[num - 1]);
}
}
for (int i = 0; i < nums.length; ++i) {
if (nums[i] > 0) {
return i + 1;
}
}
return nums.length + 1;
}
}