# 163. Missing Ranges

Given a sorted integer array  nums , where the range of elements are in the inclusive range [ lowerupper ], 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"]
``````

``````class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
vector<string> res;
for (int num : nums) {
if (num > lower) res.push_back(to_string(lower) + (num - 1 > lower ? ("->" + to_string(num - 1)) : ""));
if (num == upper) return res;
lower = num + 1;
}
if (lower <= upper) res.push_back(to_string(lower) + (upper > lower ? ("->" + to_string(upper)) : ""));
return res;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/163

Summary Ranges

https://leetcode.com/problems/missing-ranges/

https://leetcode.com/problems/missing-ranges/discuss/50468/Accepted-Java-solution-8-lines-and-0ms

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation