My C++ solution


  • 0
    G
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        nums.insert(nums.begin(), lower-1);
        nums.push_back(upper+1);
        vector<string> ret;
        
        int end = 1;
        while(end < nums.size()){
            if(nums[end] - 1 != nums[end-1]){
                string s1 = to_string(nums[end-1]+1);
                string s2 = to_string(nums[end]-1);
                if(s1 != s2){
                    s1 += "->" + s2;
                }
                ret.push_back(s1);
            }
            end++;
        }
        
        nums.erase(nums.begin());
        nums.pop_back();
        return ret;
    }

Log in to reply
 

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