- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem41.java
More file actions
Latest commit
27 lines (22 loc) · 662 Bytes
/
Copy pathProblem41.java
File metadata and controls
27 lines (22 loc) · 662 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
importjava.util.*;
classProblem41 {
publicstaticvoidmain(String[] args) {
int[] nums = newint[]{1,3,2,4};
System.out.println(newProblem41().firstMissingPositive(nums));
}
publicintfirstMissingPositive(int[] nums) {
// 找出最大值,并将其作为数组的长度
intmax = Integer.MIN_VALUE;
Set<Integer> s = newHashSet<>();
for (inti = 0; i < nums.length; i++) {
if (nums[i] > 0) s.add(nums[i]);
if (nums[i] > max) max = nums[i];
}
for (intj = 1; j <= max; j++) {
if (!s.contains(j)) {
returnj;
}
}
returnmax <= 0 ? 1 : max+1;
}
}