[LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
这道题给定我们一个有序数组,让我们总结区间,具体来说就是让我们找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“->"。我们需要两个变量i和j,其中i是连续序列起始数字的位置,j是连续数列的长度,当j为1时,说明只有一个数字,若大于1,则是一个连续序列,代码如下:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int i = , n = nums.size();
while (i < n) {
int j = ;
while (i + j < n && (long)nums[i + j] - nums[i] == j) ++j;
res.push_back(j <= ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - ]));
i += j;
}
return res;
}
};
类似题目:
Data Stream as Disjoint Intervals
参考资料:
https://leetcode.com/problems/summary-ranges/
https://leetcode.com/problems/summary-ranges/discuss/63451/9-lines-c%2B%2B-0ms-solution
https://leetcode.com/problems/summary-ranges/discuss/63219/Accepted-JAVA-solution-easy-to-understand
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Summary Ranges 总结区间的更多相关文章
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- LeetCode——Summary Ranges
Description: Given a sorted integer array without duplicates, return the summary of its ranges. For ...
- (leetcode)Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...
- Leetcode228. Summary Ranges汇总区间
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4->5",& ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
随机推荐
- IntelliJ IDEA 内存优化最佳实践
本文作者在和同事的一次讨论中发现,对 IntelliJ IDEA 内存采用不同的设置方案,会对 IDE 的速度和响应能力产生不同的影响. Don't be a Scrooge and give you ...
- iOS UINavigationController(内容根据iOS编程编写)
我们知道 UITabBarController 对象,可以通过使用该对象,用户可以切换不同的屏幕.当要切换的各个屏幕之间没有相互依存关系的时候,该对象可以很好的完成任务.但是当多个屏幕互有关系的时候, ...
- 【原创】如何确定Kafka的分区数、key和consumer线程数
在Kafak中国社区的qq群中,这个问题被提及的比例是相当高的,这也是Kafka用户最常碰到的问题之一.本文结合Kafka源码试图对该问题相关的因素进行探讨.希望对大家有所帮助. 怎么确定分区数? ...
- 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(三)
(三) 搭建Windows Azure Pack环境 1安装SQL SERVER 2012 服务器 为简单起见,本例直接使用了Azure提供的具有SQLServer的Win2012 Server镜像来 ...
- PHP中this,self,parent三个关键字
this,self,parent三个关键字从字面上比较好理解,分别是指这.自己.父亲. this是指向当前对象的指针(姑且用C里面的指针来看吧)self是指向当前类的指针parent是指向父类的指针( ...
- j2ee log4j集中式日志解决方案logpool-v0.2
下一个小版本会进行清理. war包下载地址 http://pan.baidu.com/s/1nvGmORn
- xinetd cpu 100%
今天,有个给客户试用的环境出现xinetd cpu 100%,而且连续运行很长时间了.之前也有环境发生过,今天排查解决了三四个问题,实在是查的身体都不舒服了,还没时间查这个问题... 知道的求解...
- JDBC数据库访问操作的动态监测 之 Log4JDBC
log4jdbc是一个JDBC驱动器,能够记录SQL日志和SQL执行时间等信息.log4jdbc使用SLF4J(Simple Logging Facade)作为日志系统. 特性: 1.支持JDBC3和 ...
- 获得设备的唯一标识符UDID
在IOS5之后,苹果为避免根据UDID获得用户的信息,而禁止使用uniqueIdentifier获得UDID,但是仍有些应用需要根据UDID区分设备 有一个系统的库IOKit.framework可以获 ...
- ViewPager轮播图
LoopViewPagerLayout无限轮播 项目地址:https://github.com/why168/LoopViewPagerLayout 支持三种动画: 支持修改轮播的速度: 支持修改滑动 ...