[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 ...
随机推荐
- 小萝贝控机大师工具推荐(一款在PC就能控制手机界面的工具)
在一次写博客的过程中,要截取手机app上的几张图片,然后粘贴到博客里面去,不了解这个工具的时候,我就从手机上截图(使用其他的截图app或者使用手机自己的截图功能),然后再传送到电脑上,然后再放到博文中 ...
- Java对象序列化剖析
对象序列化的目的 1)希望将Java对象持久化在文件中 2)将Java对象用于网络传输 实现方式 如果希望一个类的对象可以被序列化/反序列化,那该类必须实现java.io.Serializable接口 ...
- 在Application_Error事件中获取当前的Action和Control
ASP.NET MVC程序处理异常时,方法有很多,网上也有列举了6种,下面是使用全局处理在Global.asax文件的Application_Error事件中实现.既然是ASP.NET MVC,我需要 ...
- Java语言中的面向对象特性总结
Java语言中的面向对象特性 (总结得不错) [课前思考] 1. 什么是对象?什么是类?什么是包?什么是接口?什么是内部类? 2. 面向对象编程的特性有哪三个?它们各自又有哪些特性? 3. 你知 ...
- 充电时间 Go中的数组、切片、map简单示例
数组是固定长度的,依稀让我想起了VB:切片是动态的:map一般是键值对 package main import ( "fmt" ) func main() { var userna ...
- Java01
1.JAVA历史概述 百度百科:http://baike.baidu.com/view/29.htm 詹姆斯.高斯林 (高司令)----java之父 Sun Micros ...
- glibc 各版本发布时间以及内核默认glibc版本
最近有些软件要求glibc 2.14+,centos 6.x自带的版本是2.12的,特查了下glibc 各版本发布时间以及与对应的内核,如下: Complete glibc release histo ...
- SpringBootService,一个基于spring boot搭建的SOA服务框架
SpringBootService,这是一个spring boot微服务的框架,包括redis,mq,restful,定时器,mybatis.易扩容.易维护的架构. 项目说明 该项目使用maven进行 ...
- Vue基本应用
1. returnDetail.$mount('#returnDetail'); 不用el 直接可以绑定数据到页面的id上 作用区域不能交叠多个vue 实体 否则后面的vue 实体会失效. 2. ...
- co源码解读
背景: 闲来无事,翻了下co的源码来看,源码短小精悍,算上注释,一共240行左右: 决定写一篇博客来记录下学习的心得. TJ大神的co:https://github.com/tj/co 作用: co通 ...