[LeetCode228]Summary Ranges

题目:

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> vecStr;
        if(nums.size() == 1)
        {
            vecStr.push_back(to_string(nums[0]));
            return vecStr;
        }
        for(int i = 0; i < nums.size(); ++i)
        {
            int a = nums[i];
            while( i+1 < nums.size() && (nums[i+1] - nums[i]) == 1 )
            {
                ++i;
            }
            if(a != nums[i])
            {
                vecStr.push_back(to_string(a) + "->" + to_string(nums[i]));
            }
            else if(a == nums[i])
                vecStr.push_back(to_string(a));
        }
        return vecStr;
    }
};

 

你可能感兴趣的:([LeetCode228]Summary Ranges)