Skip to content

Commit 0f69c55

Browse files
solves #163: Missing ranges in java
1 parent 59c35f0 commit 0f69c55

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | [![Java](assets/java.png)](src/IntersectionOf2LinkedLists.java) [![Python](assets/python.png)](python/intersecction_of_two_linked_lists.py) | |
157157
| 161 | 🔒 [One Edit Distance](https://leetcode.com/problems/one-edit-distance) | | |
158158
| 162 | [Find Peak Element](https://leetcode.com/problems/find-peak-element) | [![Java](assets/java.png)](src/FindPeakElement.java) | |
159+
| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges) | [![Java](assets/java.png)](src/MissingRanges.java) | |
159160
| 164 | [Maximum Gap](https://leetcode.com/problems/maximum-gap) | | |
160161
| 165 | [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers) | [![Java](assets/java.png)](src/CompareVersionNumbers.java) | |
161162
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal) | [![Java](assets/java.png)](src/FractionToRecurringDecimal.java) | |

src/MissingRanges.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)