C++ 9 lines easy to understand


  • 0
    M
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> ranges;
            for (int i = 0; i < nums.size(); i++) {
                string range = to_string(nums[i]);
                int start = nums[i];
                while (i+1 < nums.size() && nums[i+1] == nums[i]+1) i++;
                if (nums[i] != start) range += "->" + to_string(nums[i]);
                ranges.push_back(range);
            }
            return ranges;
        }
    };
    

Log in to reply
 

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