Summary Ranges
2015-06-27 18:39
369 查看
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given
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; if (nums.empty()) { return res; } int begin = 0, end = 1; while (begin < nums.size()) { if ((end < nums.size() && nums[end] != nums[end - 1] + 1) || end == nums.size()) { string range; if (end - begin == 1) { range = to_string(nums[begin]); } else { range = to_string(nums[begin]) + "->" + to_string(nums[end - 1]); } res.push_back(range); begin = end; } end++; } return res; } };
相关文章推荐
- FPGA图像处理系列——基于ZEDBoard构建图像处理通路(Block Ram版本)
- iOS:关于获取网络类型和运营商信息
- 读博:做科研的职业训练
- Codeforces 553B Kyoya and Permutation
- Java基础——线程(一)
- MATLAB的变量、向量和矩阵的定义与赋值
- 04-树8. Complete Binary Search Tree (30)
- 集群
- c++_1st par
- 集群
- 决定你人生命运的10年,你做了什么?
- 装win7和Ubuntu双系统
- Javascript之创建对象(工厂模式与构造函数模式)
- Skype for Business Server 2015-11-Web Application Proxy-部署
- Skype for Business Server 2015-11-Web Application Proxy-部署
- 音频焦点 (audio focus)(二)
- 排序1-冒泡,选择和插入
- LeetCode之“链表”:Sort List
- HDU 1018 Big Number (阶乘位数)
- 超级2048