码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode- Given a sorted integer array without dup

时间:2015-11-08 16:36:16      阅读:234      评论: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"].

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;
    }
};


leetcode- Given a sorted integer array without dup

标签:

原文地址:http://my.oschina.net/u/2368952/blog/527685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!