-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMissingRanges.java
More file actions
37 lines (30 loc) · 935 Bytes
/
MissingRanges.java
File metadata and controls
37 lines (30 loc) · 935 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
37
package interval;
import java.util.ArrayList;
import java.util.List;
/**
* Description: https://leetcode.com/problems/missing-ranges
* Difficulty: Easy
* Time complexity: O(n)
* Space complexity: O(1)
*/
public class MissingRanges {
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
List<List<Integer>> ranges = new ArrayList<>();
if (nums.length == 0) {
ranges.add(List.of(lower, upper));
return ranges;
}
if (nums[0] != lower) {
ranges.add(List.of(lower, nums[0] - 1));
}
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1] + 1) {
ranges.add(List.of(nums[i - 1] + 1, nums[i] - 1));
}
}
if (nums[nums.length - 1] != upper) {
ranges.add(List.of(nums[nums.length - 1] + 1, upper));
}
return ranges;
}
}