标签:
直接做
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> ans;
if (nums.size() == 0) return ans;
int startPosition = 0, nowPosition = 1;
for (; nowPosition <= nums.size(); nowPosition++) {
if (nowPosition == nums.size() || nums[nowPosition] > nums[nowPosition - 1] + 1) {
if (startPosition != nowPosition - 1) {
ans.push_back(toS(nums[startPosition]) + "->" + toS(nums[nowPosition - 1]));
}
else {
ans.push_back(toS(nums[startPosition]));
}
startPosition = nowPosition;
}
}
return ans;
}
string toS(int i) {
char t[20];
string s;
sprintf(t, "%d", i);
s = t;
return s;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u012925008/article/details/46848049