228 汇总区间

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        int l = 0;
        int r = 0;
        vector<string> result;
        while (r < nums.size()) {
            // find interval
            while (r + 1 < nums.size() && nums[r] + 1 == nums[r+1]) {
                r++;
            }
            // save result
            if (l != r) {
                result.push_back(to_string(nums[l]) + "->" + to_string(nums[r]));
            } else {
                result.push_back(to_string(nums[l]));
            }
            // update l, r
            r = r + 1;
            l = r;
        }
        return result;
    }
};
上一篇:第 228 场周赛


下一篇:228周赛T2