forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_163.java
34 lines (32 loc) · 1.1 KB
/
_163.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 163. Missing Ranges
*
* Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
* For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
*/
public class _163 {
public static class Solution1 {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> result = new ArrayList<>();
long low = (long) lower - 1;
long up = 0;
for (int i = 0; i <= nums.length; i++) {
if (i == nums.length) {
up = (long) upper + 1;
} else {
up = nums[i];
}
if (up == low + 2) {
result.add(low + 1 + "");
} else if (up > low + 2) {
result.add((low + 1) + "->" + (up - 1));
}
low = up;
}
return result;
}
}
}