给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总。

示例 1:

输入: [0,1,2,4,5,7]
输出: ["0->2","4->5","7"]
示例 2:

输入: [0,2,3,4,6,8,9]
输出: ["0","2->4","6","8->9"]

详见:https://leetcode.com/problems/summary-ranges/description/

Java实现:

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res=new ArrayList<String>();
int n=nums.length;
int i=0;
while(i<n){
int j=1;
while(i+j<n&&nums[i+j]-nums[i]==j){
++j;
}
res.add(j==1?String.valueOf(nums[i]):String.valueOf(nums[i])+"->"+String.valueOf(nums[i+j-1]));
i+=j;
}
return res;
}
}

C++实现:

class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int i=0,n=nums.size();
while(i<n)
{
int j=1;
while(i+j<n&&nums[i+j]-nums[i]==j)
{
++j;
}
res.push_back(j==1?to_string(nums[i]):to_string(nums[i])+"->"+to_string(nums[i+j-1]));
i+=j;
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/4603555.html

228 Summary Ranges 汇总区间的更多相关文章

  1. [LeetCode] 228. Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  2. Leetcode228. Summary Ranges汇总区间

    给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4->5",& ...

  3. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  4. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

  5. 【刷题-LeetCode】228. Summary Ranges

    Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...

  6. LeetCode 228. Summary Ranges (总结区间)

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  7. [leetcode]228. Summary Ranges区间统计

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  8. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  9. Java for LeetCode 228 Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

随机推荐

  1. OpenJudge百炼习题解答(C++)--题3142:球弹跳高度的计算

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 一球从某一高度落下(整数,单位米),每次落地后反跳回原来高度的一半.再落下. 编程计算气球在第10次落地时,共经过多少米? ...

  2. Xpath—解决这个问题的良药

    何为良药? 因为在XML中存在一些问题和缺陷,针对这些问题就产生了响应的解决方式.如: getElementById方法在解析XML时因为一些原因适不适合的: 首先XML中每一个元素节点不一定有id属 ...

  3. The Pilots Brothers&#39; refrigerator-DFS路径打印

    I - The Pilots Brothers' refrigerator Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format ...

  4. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) E.Passwords AC自动机+dp

    题目链接:点这里 题意: 让你构造一个长度范围在[A,B]之间 字符串(大小写字母,数字),问你有多少种方案 需要满足条件一下: 1:构成串中至少包含一个数字,一个大写字母,一个小写字母:   2:不 ...

  5. 暴走吧!Snapdragon SDK开发速成指南

    (文/Aurora J) Qualcomm的Snapdragon处理器.它快如闪电.效率极高.擅长挑战多任务极限,而且拥有攻城狮们梦寐以求的无限潜能.它能确保您的手机集4G LTE.极速体验.长久续航 ...

  6. CentOS 7.2 源码安装Python3.6

    1.环境 安装CentOS 7.2最小系统(CentOS-7-x86_64-Minimal-1511.iso) 2.需求 Python-3.6.4.tar.xz(官网下载) GCC(yum安装) 一堆 ...

  7. Spring-AOP解析

    策略模式:选择动态代理还是CGLIB方式: 1.这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. 2.AOP基本上是通过代理机制实现的 3.写好验证用户的代码,然 ...

  8. [noip模拟赛]小U的女装

    https://www.zybuluo.com/ysner/note/1329304 题面 有一张\(n\)点\(m\)边的.不一定联通的无向图. 如果选了一条边,就不能选其两个端点. 现在同时选点和 ...

  9. #!/usr/bin/env ruby 与 #!/usr/bin/ruby 的区别(copy)

    [说明:资料来自http://blog.csdn.net/wh_19910525/article/details/8040494] 脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执 ...

  10. CentOS7.5 -- Ansible部署与应用

    第1章 Ansible概述 Ansible是一个配置管理系统configuration management system python 语言是运维人员必须会的语言 ansible 是一个基于pyth ...