Concise c++ code valid for range [INT_MIN, INT_MAX]


  • 0
    X
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> result;
        int start, i, j, size = nums.size();
        if(size == 0)
            return vector<string>({genString(lower, upper)});
        for(start = lower, i = 0; i < size; ) {
            for( ; i < size && start < upper && start == nums[i]; start++, i++);
            if(i < size && start < upper) {
                result.push_back(genString(start, nums[i] - 1));
                start = nums[i];
            }
            else {
                if(nums[size - 1] != upper)
                    result.push_back(genString(start, upper));
                break;
            }
        }
        return result;
    }
    
    string genString(int start, int end) {
        return to_string(start) + (start < end ? "->" + to_string(end) : "");
    }

Log in to reply
 

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