-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-163-Missing-Ranges.java
87 lines (73 loc) · 2.79 KB
/
LeetCode-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Solution {
// 1.
/*
https://leetcode.com/problems/missing-ranges/discuss/50476/Accepted-Java-solution-with-explanation
This solution is not so intuitive and would be difficult to come up with during the interview.
The top-submitted solution (beats 100%) is much easier to understand and implement in 3 simple steps.
Find the range between lower and first element in the array.
Find ranges between adjacent elements in the array.
Find the range between upper and last element in the array.
*/
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
result.add(formRange(lower, upper));
return result;
}
// 1st step, add [lower, nums[0]]
if (lower < nums[0]) {
result.add(formRange(lower, nums[0] - 1));
}
// 2nd step
/*
Input
[-2147483648,-2147483648,0,2147483647,2147483647]
-2147483648
2147483647
Output
["-2147483647->-1","1->2147483646","-2147483648->2147483646"]
Expected
["-2147483647->-1","1->2147483646"]
We must check if "nums[i - 1] == nums[i]""
*/
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i - 1] != nums[i] && nums[i - 1] + 1 < nums[i]) {
result.add(formRange(nums[i - 1] + 1, nums[i] - 1));
}
}
// 3rd step
if (nums[nums.length - 1] < upper) {
result.add(formRange(nums[nums.length - 1] + 1, upper));
}
return result;
}
private String formRange(int low, int hi) {
return low == hi ? String.valueOf(low) : low + "->" + hi;
}
// 2. Similar
// public List<String> findMissingRanges(int[] nums, int lower, int upper) {
// List<String> result = new ArrayList<>();
// if (nums == null || nums.length == 0){
// result.add(formRange(lower,upper));
// return result;
// }
// // 1st step
// if (nums[0] > lower){
// result.add(formRange(lower,nums[0]-1));
// }
// // 2nd step
// for (int i = 0; i < nums.length-1; i++){
// if (nums[i+1] != nums[i] && nums[i+1] > nums[i] +1) {
// result.add(formRange(nums[i]+1, nums[i+1]-1));
// }
// }
// // 3rd step
// if (nums[nums.length-1] < upper){
// result.add(formRange(nums[nums.length-1]+1, upper));
// }
// return result;
// }
// public String formRange(int low, int high){
// return low == high ? String.valueOf(low) : (low + "->" + high);
// }
}