C++ solution, easy


  • 0
    M
    class Solution {
    public:
        vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
            int next = lower;
            vector<string> result;
            for(int i=0;i<nums.size();i++){
                if(nums[i]<next)
                    continue;
                if(nums[i] == next){
                    next++;
                    continue;
                }
                if(next != nums[i]-1)
                result.push_back(to_string(next)+"->"+to_string(nums[i]-1));
                else
                result.push_back(to_string(next));
                next = nums[i]+1;
            }
            if(next<=upper){
                if(next != upper)
                result.push_back(to_string(next)+"->"+to_string(upper));
                else
                result.push_back(to_string(next));
            }
            return result;
        }
    };

Log in to reply
 

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