File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 156
156
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | [](src/IntersectionOf2LinkedLists.java) [](python/intersecction_of_two_linked_lists.py) | |
157
157
| 161 | 🔒 [One Edit Distance](https://leetcode.com/problems/one-edit-distance) | | |
158
158
| 162 | [Find Peak Element](https://leetcode.com/problems/find-peak-element) | [](src/FindPeakElement.java) | |
159
+ | 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges) | [](src/MissingRanges.java) | |
159
160
| 164 | [Maximum Gap](https://leetcode.com/problems/maximum-gap) | | |
160
161
| 165 | [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers) | [](src/CompareVersionNumbers.java) | |
161
162
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal) | [](src/FractionToRecurringDecimal.java) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/missing-ranges
2
+ // T: O(N)
3
+ // S: O(N)
4
+
5
+ import java .util .ArrayList ;
6
+ import java .util .List ;
7
+
8
+ public class MissingRanges {
9
+ public List <List <Integer >> findMissingRanges (int [] nums , int lower , int upper ) {
10
+ if (nums .length == 0 ) {
11
+ return List .of (List .of (lower , upper ));
12
+ }
13
+
14
+ final List <List <Integer >> result = new ArrayList <>();
15
+ if (lower != nums [0 ]) {
16
+ result .add (List .of (lower , nums [0 ] - 1 ));
17
+ }
18
+ for (int i = 0 ; i < nums .length - 1 ; i ++) {
19
+ if (nums [i + 1 ] - nums [i ] > 1 ) {
20
+ result .add (List .of (nums [i ] + 1 , nums [i + 1 ] - 1 ));
21
+ }
22
+ }
23
+ if (nums [nums .length - 1 ] != upper ) {
24
+ result .add (List .of (nums [nums .length - 1 ] + 1 , upper ));
25
+ }
26
+ return result ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments