首页 > 其他 > 详细

Summary Ranges leetcode

时间:2016-01-09 16:48:13      阅读:215      评论:0      收藏:0      [点我收藏+]

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.

 

Subscribe to see which companies asked this question

 
vector<string> summaryRanges(vector<int>& nums) {
    vector<string> ret;
    if (nums.size() == 0)
        return ret;
    int i = 0;
    int beg = nums[i];
    while (i < nums.size())
    {
        if (i + 1 == nums.size() || nums[i+1] != nums[i] + 1)
        {
            if (nums[i] != beg)
                ret.push_back(to_string(beg) + "->" + to_string(nums[i]));
            else
                ret.push_back(to_string(beg));
            if(i + 1 < nums.size())
                beg = nums[i + 1];
        }
        i++;
    }
    return ret;
}

 

Summary Ranges leetcode

原文:http://www.cnblogs.com/sdlwlxf/p/5116648.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!