-
Notifications
You must be signed in to change notification settings - Fork 1
/
163. Missing Ranges.java
42 lines (37 loc) · 1.04 KB
/
163. Missing Ranges.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
35
36
37
38
39
40
41
42
//O(n) - BEST OPTIMAL
class Solution {
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
for(int i=0;i<=nums.length;i++)
{
int lim1 = 0;
int lim2 = 0;
if(i==0)
lim1 = lower;
else
lim1 = nums[i-1] +1;
if(i==nums.length)
lim2 = upper;
else
lim2 = nums[i] - 1;
if(lim1>lim2)
{
}
else if(lim1 == lim2)
{
ArrayList<Integer> list = new ArrayList<>();
list.add(lim1);
list.add(lim2);
res.add(list);
}
else
{
ArrayList<Integer> list = new ArrayList<>();
list.add(lim1);
list.add(lim2);
res.add(list);
}
}
return res;
}
}