163 Missing Ranges ****
Given a sorted integer arraynums, where the range of elements are in theinclusive range[lower,upper], return its missing ranges.
Example:
Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output: ["2", "4->49", "51->74", "76->99"]
崩溃
临界条件
必须用LONG 放LOW AND HIGH
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> ans = new ArrayList<>();
if (nums == null) {
return ans;
}
//in case of
long low = lower;
long high = upper;
for (int num : nums) {
if (num == low) {
low++;
} else if ( low < num ) {
if (low + 1 == num) {
ans.add("" + low);
} else {
ans.add(low + "->" + (num - 1));
}
low = (long)num + 1;
////////////////////this step is essential;
}
}
if (low == high) {
ans.add("" + low);
} else if (low < high) {
ans.add(low + "->" + high);
}
return ans;
}
}