leetcode- Given a sorted integer array without dup

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

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

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        int low, high, len=nums.size();
        vector<string> res;
        string fromLowToHigh;
        
        for(int i=0; i<len; i++)
        {
            low = nums[i];
            while(i+1<len&&nums[i+1] == nums[i]+1)
            {
                i++;
            } 
            high = nums[i]; 
            if(low != high) 
            {
                fromLowToHigh = to_string(low) +"->" + to_string(high);
            }
            else
            {
                fromLowToHigh = to_string(low);
            }
            res.push_back(fromLowToHigh);
        }
      
        return res;
    }
};
相關文章
相關標籤/搜索