C++_Accepted


  • 0

    Update:

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

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

Log in to reply
 

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