ID | Title | Difficulty | |
---|---|---|---|
Loading... |
163. Missing Ranges
Easy
LeetCode
Array
Problem
Given a sorted integer array nums, where the range of elements are in the inclusive 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"]
Code
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> res = new ArrayList<>();
long alower = (long)lower;
long aupper = (long)upper;
for(int num : nums){
if(alower == num){
alower++;
} else if(alower < num){
if(alower + 1 == num){
res.add(String.valueOf(alower));
} else {
res.add(alower + "->" + (num - 1));
}
alower = (long)num + 1;
}
}
if(alower == aupper){
res.add(String.valueOf(alower));
} else if (alower < aupper){
res.add(alower + "->" + aupper);
}
return res;
}
}
按 <- 键看上一题!
162. Find Peak Element
按 -> 键看下一题!
164. Maximum Gap