-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_163.java
More file actions
36 lines (32 loc) · 1004 Bytes
/
Copy pathP_163.java
File metadata and controls
36 lines (32 loc) · 1004 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
32
33
34
35
36
package leetcode.medium;
import java.util.ArrayList;
import java.util.List;
public class P_163 {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
final List<String> list = new ArrayList<>();
if (nums.length == 0) {
helper((long) lower - 1, (long) upper + 1, list);
return list;
}
for (int i = 0; i < nums.length + 1; i++) {
if (i == 0) {
helper((long) lower - 1, nums[0], list);
} else if (i == nums.length) {
helper(nums[nums.length - 1], (long) upper + 1, list);
} else {
helper(nums[i - 1], nums[i], list);
}
}
return list;
}
public void helper(long a, long b, List<String> list) {
if (b <= a + 1) {
return;
}
if (b == a + 2) {
list.add(String.valueOf(a + 1));
} else {
list.add((a + 1) + "->" + (b - 1));
}
}
}