leetcode-Summary Ranges

 Difficulty: Easy

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> res;
        if(nums.empty())
            return res;
        int begin=0,end;
        int size=nums.size();
        
        for(end=0;end<size;++end){
            string temp(to_string(nums[begin]));
            while(end+1<size&&nums[end]+1==nums[end+1])
                ++end;
            if(begin!=end)
                temp+="->"+to_string(nums[end]);
            res.push_back(temp);
            begin=end+1;
        }
        return res;
    }
};


你可能感兴趣的:(leetcode-Summary Ranges)