forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingRanges.java
More file actions
33 lines (30 loc) · 830 Bytes
/
MissingRanges.java
File metadata and controls
33 lines (30 loc) · 830 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
import java.util.LinkedList;
import java.util.List;
/**
* TestCases
* [], lower=1, upper=1
* [2147483647], lower=0,upper=2147483647
* 注意溢出的问题
*/
public class MissingRanges {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> list = new LinkedList<String>();
long next = lower;
for (int n : nums) {
if (n < next) {
continue;
}
if (n > next) {
list.add(getRange(next, n - 1));
}
next = (long) n + 1;
}
if (upper >= next) {
list.add(getRange(next, upper));
}
return list;
}
private String getRange(long start, int end) {
return start == end ? String.valueOf(start) : start + "->" + end;
}
}