Sharing my simple C++ solution


  • 0
    T
    class Solution {
    public:
        vector<string> summaryRanges(vector<int>& nums) {
            vector<string> result;
            int n = nums.size();
            int start=0;
            int end = 0;
            while(end<n)
            {
                while(end+1<n && nums[end+1]-nums[end]==1)
                {
                    end++;
                }
                
                if(start==end)
                    result.push_back(to_string(nums[start]));
                else
                {
                    string s = to_string(nums[start]);
                    s = s + "->";
                    s = s + to_string(nums[end]);
                    result.push_back(s);
                }
                
                start = end+1;
                end = start;
            }
            
            return result;
        }
    };

Log in to reply
 

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