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 ...
随机推荐
- Django框架基础-MTV模型
一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...
- 外引js — 先引入cdn,cdn失效时引入本地js
参考:http://www.tianshan277.com/563.html 效果: html: <!DOCTYPE html> <html lang="en"& ...
- JPA默认方法查询遇到转JSON的处理
JPA提供的findAll等查询方法在有关联的对象时 比如:在查userInfo @Entity@Table(name = "user_info")public class Use ...
- Johnson–Lindenstrauss 定理-Johnson–Lindenstrauss lemma
Johnson–Lindenstrauss 定理是这样的:一个一百万维空间里的随便一万个点,一定可以几乎被装进一个几十维的子空间里! 严格说来是这样:在 M 维空间中的 N 个点,几乎总是被包含在一个 ...
- 02.MyBatis在DAO层开发使用的Mapper动态代理方式
在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...
- C++ 类设计核查表
参考:https://www.jianshu.com/p/01601515ca31 <大规模C++程序设计> 函数接口: 1.运算符或非运算符函数? 2.自由或成员运算符? 3.虚函数或非 ...
- 「题解」:07.18NOIP模拟赛T1:星际旅行
问题 A: 星际旅行 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 考试心路历程 拿到这道题感觉很懵逼,所以先搞的T2和T3,最后码了个暴力,结果还不如直接输出‘0’得分高 ...
- 小程序关闭时暂停webview里的音乐
document.addEventListener("visibilitychange", () => { if(document.hidden) { // 页面被 ...
- JavaScript中字符串类型
字符串类型 字符串介绍 这是程序里面使用最为广泛的一-种类型.在JavaScript里面, 可以使用单引号,也可以使用双引号: 字符串这种数据类型非常霸道,它和其他数据类型相加都会被转换后才为字符串类 ...
- php面向对象成员方法(函数)练习
<?php header('content-type:text/html;charset=utf-8'); //成员方法的举例 /* ①添加sayHello 成员方法,输出 'hello' ②添 ...