[LeetCode] Missing Ranges

Problem Description:

Given a sorted integer array where the range of elements are [lowerupper] 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"].

Well, I guess this problem aims at testing your ability to think throughly. The tricky part in this problem is to take all possible cases into consideration and return the result correctly.

Of course, you also need to unify the boundary cases to give a succinct code.

The code is as follows.

 1 class Solution {
 2 public:
 3     vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
 4         vector<string> ranges;
 5         int pre = lower - 1, n = nums.size();
 6         for (int i = 0; i <= n; i++) {
 7             int cur = (i == n) ? upper + 1 : nums[i];
 8             if (cur - pre >= 2)
 9                 ranges.push_back(missingRanges(pre + 1, cur - 1));
10             pre = cur;
11         }
12         return ranges;
13     }
14 private:
15     string missingRanges(int low, int up) {
16         return (low == up) ? to_string(low) : to_string(low) + "->" + to_string(up);
17     }
18 };
时间: 2024-12-22 17:34:06

[LeetCode] Missing Ranges的相关文章

[LeetCode] Missing Ranges 缺失区间

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

LeetCode Missing Ranges [LeetCode Book Problem]

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-

[LeetCode] 163. Missing Ranges 缺失区间

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-

leetcode[163]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-

Missing Ranges -- LeetCode

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-

【LeetCode】Missing Ranges

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&q

163. 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", "

[Locked] Missing Ranges

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&q

Missing Ranges 解答

Question Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51-&