Leetcode #228 Summary Ranges
2015-08-01 21:48
309 查看
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given
Credits:
Special thanks to
@jianchao.li.fighter for adding this problem and creating all test cases.
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.
class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> result; int start, index = 0; while(index < nums.size()){ start = nums[index]; while(++index < nums.size() && nums[index] == nums[index - 1] + 1); if (start == nums[index - 1]) result.push_back(to_string(start)); else result.push_back(to_string(start) + "->" + to_string(nums[index - 1])); } return result; } };
相关文章推荐
- 线性判别分析(Linear Discriminant Analysis)
- MySQL5.6.26免安装步骤
- Oracle 学习之RMAN(十四)恢复实战--基于时间点恢复
- Ceph学习之Crush算法—— Bucket
- linux进程一章知识总结
- jsp页面与jsp页面之间的传值
- 九度oj 1444
- 把线性方程组变系数矩阵和常量形式
- 黑马程序员------网络编程(TCP&UDP)
- 公交车比喻
- 说反话(c++实现)
- nutch2.2.1 mysql 建表语句
- 说反话(c++实现)
- Decode Ways
- PHP实例开发(2)PHP通过mail()或Socket发邮件
- EFCode First 导航属性
- 斐波那契数列问题
- pandas groupby (TimeGrouper)重写Q3
- java 微信开发
- 探索angular源码--启动(1)