C++ solution, easy understanding


  • 0
    L
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        const int size_n = nums.size();
        int start = lower;
        for (int i = 0; i < size_n;) {
            while (i < size_n && start == nums[i]) { i++; start++;}
            if (start >= upper+1) return res;
            if (i == size_n) break;
            res.push_back(getRange(start, nums[i]-1));
            start = nums[i];
        }
        if (nums.empty() || nums[size_n - 1] < upper) {
            res.push_back(getRange(start, upper));
        }
        return res;
    }
    string getRange(int start, int end) {
        string str = to_string(start);
        if (start < end) str += "->" + to_string(end);
        return str;
    }

Log in to reply
 

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