Accepted C++ solution


  • 0
    F
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        int p = lower - 1;
        int q;
        for (int i = 0; i < nums.size(); i++) {
            int ni = nums[i];
            if (ni < lower) {
                continue;
            } else if (ni > upper) {
                break;
            } else {
                q = ni;
                if (q - p > 1) {
                    res.push_back(trans(p, q));
                } 
                p = q;
            }
        }
        if (upper - p > 0) {
            res.push_back(trans(p, upper + 1));
        } 
        return res;
    }    
    string trans(int p, int q) {
        string s;
        if (q - p == 2) {
            s = to_string(p + 1);
        } else {
            s = to_string(p + 1) + "->" + to_string(q - 1);
        }
        return s;
    }

Log in to reply
 

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