Leetcode228. Summary Ranges汇总区间
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
示例 1:
输入: [0,1,2,4,5,7] 输出: ["0->2","4->5","7"] 解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。
示例 2:
输入: [0,2,3,4,6,8,9] 输出: ["0","2->4","6","8->9"] 解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums)
{
int len = nums.size();
vector<string> res;
if(len == 0)
return res;
if(len == 1)
{
res.push_back(to_string(nums[0]));
return res;
}
int start = 0;
int end = 0;
for(int i = 0; i < len; i++)
{
end = i;
if(i == 0)
continue;
if(nums[i] - nums[i - 1] != 1)
{
if(start == end - 1)
res.push_back(to_string(nums[start]));
else
res.push_back(to_string(nums[start]) + "->" + to_string(nums[end - 1]));
start = i;
}
if(i == len - 1)
{
if(start != end)
res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else
res.push_back(to_string(nums[start]));
}
}
return res;
}
};
Leetcode228. Summary Ranges汇总区间的更多相关文章
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- [LeetCode228]Summary Ranges
题目: Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- Missing Ranges & Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
随机推荐
- pycharm IDE在导入自定义模块时提示有错,但实际没错
在建立python项目时,有时为了区分资源和代码,如在项目文件夹下新建img和src两个文件夹,这时导入自定义模块会提示错误,结果没错但感觉别扭.如: 这是因为pycharm提示功能是从根目录上去寻找 ...
- [笔记]180612 for DevOps
adb devices 识别不了安卓手机:我下的adb interface驱动下载链接:如果设备管理器中ADB Interface是黄色的,就需要先安装adb interface驱动(BD:adb i ...
- postgresql计算2个日期之间工作日天数的方法
select date_part( 'day', minus_weekend(begin_date,end_date)) from table1 where name in ('a', 'b', 'c ...
- 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...
- 【BZOJ4561】[JLoi2016]圆的异或并
传送门 把圆拆成上下两个圆弧,因为不存在相交关系,圆弧直接的上下关系是不变的. 用set维护这些圆弧,插入的时候upper_bound一下,如果找到的是上圆弧,就是我外面的第一个圆,否则我外面的第一个 ...
- EF 中获取 TableAttribute的值,即数据库中真实的表名
比如EF中我定义了这样一个实体: [Table(Name = "MyTableName")] public class MyClass { } [Table(Name = &quo ...
- 菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)[转]
菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- day 46 Javascript学习
Javascript学习 JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScri ...
- Ubuntu安装Maven(转)
原文地址:http://my.oschina.net/hongdengyan/blog/150472 一.环境说明: 操作系统:Ubuntu 14.10(64位) maven:apache-maven ...
- MySQL-Utilities:mysqldbcompare及跳过复制错误
mysqldbcompare也是MySQL-Utilities工具集的一个脚本.mysqldbcompare从两个数据库比较对象和数据的不同.数据库中的对象包括:表.视图.触发器.存储过程.函数和事件 ...