Posted on

Description

Submission

class Solution {
public:
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        nums.insert(nums.begin(), lower-1);
        nums.push_back(upper+1);

        vector<string> rets;

        for(int i = 0; i < nums.size() - 1; ++i) {
            int l = nums[i] + 1;
            int r = nums[i+1] - 1;
            if(l == r) rets.push_back(to_string(l));
            else if(l < r) {
                stringstream ss;
                ss << l << "->" << r;
                rets.push_back(ss.str());
            }
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *