My simple c++ solution


  • 0
    K
    class Solution {
    public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string>res;
        string str="";
        int k=0;
        if(nums.size()==1)return {to_string(nums[0])};
        for(int i=0; i < nums.size(); i++){
            if(i < nums.size()-1 && (nums[i+1]- nums[i])==1)continue;
            else {
                if(k==i)str=to_string(nums[k]);
                else str=to_string(nums[k])+"->"+ to_string(nums[i]);
                res.push_back(str);
                k=i+1;
            }
        }
        return res;
    }
    };

Log in to reply
 

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