Missing Ranges
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> res = new ArrayList();
if(nums.length == 0){
if(lower == upper)
res.add(lower + "");
else res.add(lower + "->" + upper);
return res;
}
if(lower < nums[0])
if(nums[0] - 1 == lower)
res.add(lower +"");
else res.add(lower + "->" + (nums[0] - 1));
for(int i = 1; i < nums.length; i++){
int diff = nums[i] - nums[i - 1];
if(diff > 1){
if(diff == 2)
res.add((nums[i - 1] + 1) + "");
else res.add((nums[i - 1] + 1) + "->" + (nums[i] - 1));
}
}
if(upper > nums[nums.length - 1])
if(nums[nums.length - 1] + 1 == upper)
res.add(upper + "");
else res.add((nums[nums.length - 1] + 1) + "->" + upper);
return res;
}