My stupid C++ 0ms solution


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

Log in to reply
 

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