C++ 0ms very simple


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

Log in to reply
 

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