[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很类似,这道题让我们求缺失的空间,给了一个空间的范围[lower upper],缺失的空间的范围需要在给定的空间范围内。那么我最先想到的方法是这样的,将upper加入nums的末尾,然后用变量t初始赋值为lower,开始遍历数组nums,如果当前值和t相等,那么t自增1,如果当前值大于t,说明会有缺失区间存在,那么我们首先来判断upper是否大于当前值,如果大于,则当前值不能出现在区间里,我们还要判断当前值和t是否差1,若是,则说明缺失了一个数字t,如果不是差1,则缺失了区间[t nums[i]-1],然后我们将t赋为nums[i] + 1。如果upper小于等于当前值,说明这是最后一个缺失区间了,而且应该包含upper值,加完跳出循环即可,参见代码如下:

解法一:

class Solution {
public:
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        nums.push_back(upper);
        int t = lower;
        string out;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == t) ++t;
            else if (nums[i] > t) {
                if (upper > nums[i]) {
                    res.push_back(nums[i] - t == 1 ? to_string(t) : to_string(t) + "->" + to_string(nums[i] - 1));
                    t = nums[i] + 1;
                } else {
                    res.push_back(upper - t == 1 ? to_string(t) : to_string(t) + "->" + to_string(upper));
                    break;
                }
            }
        }
        return res;
    }
};

我们可以优化一下上面的代码,不给nums加数字,而且尽量少用一些if从句,那么我们在for循环里多增加一次循环,需要判断当i等于nums的长度时,将r赋为upper+1,否则当nums[i]小于等于upper时,将r赋为nums[i]。然后还是判断l和r的值,若相同,l自增1,否则当r大于l时,说明缺失空间存在,根据上面的判断方法相同,不过就是我们通过变量r把上面两个if情况合并了,参见代码如下:

解法二:

class Solution {
public:
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        int l = lower;
        for (int i = 0; i <= nums.size(); ++i) {
            int r = (i < nums.size() && nums[i] <= upper) ? nums[i] : upper + 1;
            if (l == r) ++l;
            else if (r > l) {
                res.push_back(r - l == 1 ? to_string(l) : to_string(l) + "->" + to_string(r - 1));
                l = r + 1;
            }
        }
        return res;
    }
};

类似题目:

Summary Ranges

参考资料:

https://leetcode.com/discuss/29206/my-concise-java-accepted-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-10-04 18:10:40

[LeetCode] Missing Ranges 缺失区间的相关文章

[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] Missing Ranges

Problem Description: 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-&g

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] Summary Ranges 总结区间

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Credits:Special thanks to @jianchao.li.fighter for adding this problem and

[Swift]LeetCode163. 缺失区间 $ 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”] 给定一个排序的整数数数组,其中元素的范围包含[0,99],返回其缺少的范围. 例如,给定[0,1,3,50,75],返回

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