LeetCode[228] Summary Ranges

来源:互联网 发布:数据漫游三星在哪 编辑:程序博客网 时间:2024/04/30 03:05

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) {vector<string> res;vector<int>::iterator front = nums.begin(), back = nums.begin();while (back != nums.end()){while (back + 1 != nums.end() && *(back + 1) == *back + 1)++back;string s;if (front == back)s = to_string(*front);elses = to_string(*front) + "->" + to_string(*back);res.push_back(s);front = back + 1, back = front;}return res;}};

0 0
原创粉丝点击