c++ solution


  • 0
    T
    class Solution {
    public:
        vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
            vector<string> ranges;
            int prev = lower - 1;
            for (int i = 0; i < nums.size(); ++i) {
                if (nums[i] == prev+1) {
                    prev = nums[i]; continue;
                }
                string str =  to_string(prev+1) + (nums[i] - prev == 2 ? "" : "->" + to_string(nums[i]-1));
                prev = nums[i];
                ranges.push_back(str);
            }
            if (upper != prev) {
                ranges.push_back(upper == prev + 1 ? to_string(upper):to_string(prev+1) + "->" + to_string(upper));
            }
            return ranges;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.