Leetcode 228. 汇总区间

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> ans;
        if(nums.empty()) return ans;
        int val=nums[0];
        for(int i=1;iif(nums[i]!=nums[i-1]+1){
                if(val==nums[i-1]) ans.push_back(to_string(val));
                else ans.push_back(to_string(val)+"->"+to_string(nums[i-1]));
                val=nums[i];
            }
        if(val==nums.back()) ans.push_back(to_string(val));
        else ans.push_back(to_string(val)+"->"+to_string(nums.back()));
        return ans;
    }
};

你可能感兴趣的:(Leetcode 228. 汇总区间)