您的位置:首页 > 其它

Leetcode #228 Summary Ranges

2015-08-01 21:48 309 查看
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"].


Credits:

Special thanks to
@jianchao.li.fighter for adding this problem and creating all test cases.

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;
        int start, index = 0;
        while(index < nums.size()){
            start = nums[index];
            while(++index < nums.size() && nums[index] == nums[index - 1]  + 1);
            if (start == nums[index - 1]) result.push_back(to_string(start));
            else result.push_back(to_string(start) + "->" + to_string(nums[index - 1]));
        }
        return result;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: