228. Summary Ranges
2016-02-21 14:07
267 查看
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.
Subscribe to see which companies asked this question
Solution:
运行速度:快
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.
Subscribe to see which companies asked this question
Solution:
class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> tmp; int i = 0; int pre = 0; if(nums.empty()) return tmp; pre = nums[0]; for(;i<nums.size();i++){ if(i+1==nums.size()){ if(pre==nums[i]) {tmp.push_back(to_string(pre));} else { tmp.push_back(to_string(pre)+"->"+to_string(nums[i])); } return tmp; } else{ if(nums[i+1]==nums[i]+1) {} else { if(pre==nums[i]) {tmp.push_back(to_string(pre));} else{ tmp.push_back(to_string(pre)+"->"+to_string(nums[i])); } pre = nums[i+1]; } } } } };心得:主要考虑corner,程序写的比较繁琐,但逻辑比较清楚
运行速度:快
相关文章推荐
- Spring 框架的设计理念与设计模式分析
- 【Linux 开发篇】之 vim编辑器
- Jetty 的工作原理以及与 Tomcat 的比较
- 3660 Cow Contest (传递闭包 floyd)
- a=aa&b=bb化为{a:"aa",b:"bb"}方法
- AQS框架
- Mysql全局ID生成方法
- Android发送消息相关类的源码解析,Handle,MessageQueue,Looper
- java中的equals和==
- java排序算法之--归并排序
- Java基础 之软引用、弱引用、虚引用
- [BZOJ3672][NOI2014]购票 树分治斜率优化
- Android开发学习笔记:浅谈WebView
- linux下I/O体系结构和设备驱动程序
- 2016年收到的第一件礼物,被评上微软全球最有价值专家MVP(二)
- php学习笔记(一)
- 【收藏】Java多线程/并发编程大合集
- iOS多线程编程的定义及方法
- viewpager+fragment实现的一个小Demo
- viewport