LeetCode228——Summary Ranges

来源:互联网 发布:135端口入侵 编辑:程序博客网 时间:2024/05/20 01:12

一个月没写C++代码,现在感到好陌生

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.

vector<string> summaryRanges(vector<int>& nums) {    vector<string> vs;    if (nums.size() == 0) return vs;     int begin = nums[0];    int end = nums[0];    char temp[256];    for (int i = 1; i < nums.size(); i++){        if (nums[i] != end+1) {            if (begin != end) {                sprintf(temp, "%d->%d", begin, end);            }else {                sprintf(temp, "%d",end);            }            string s(temp);            vs.push_back(s);            begin = nums[i];        }        end = nums[i];    }    if (begin != end) {        sprintf(temp, "%d->%d", begin, end);    }else {        sprintf(temp, "%d",end);    }    string s(temp);    vs.push_back(s);    return vs;}
0 0
原创粉丝点击