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

示例 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. Office EXCEL 中单元格怎么打斜线

    右击单元格,然后设置单元格格式,然后添加需要的边框     注意里面的文字有讲究,比如我要右上角显示Value,左下角显示Payload,则需要先输一堆空格,然后输入Value,把Value挤到右边去 ...

  2. Bootstrap的js插件之弹出框(popover)

    data-toggle="popover"--使弹出框可以切换状态: title--设置弹出框的标题: data-content--设置弹出框的内容部分: data-placeme ...

  3. c# 自定义Base16编码解码

               一.自定义Base16编码原理                  Base16编码跟Base64编码原理上有点不同,当然前面转换是一样的,都是是将输入的字符串根据默认编码转换成一 ...

  4. JS 省市区三级联动

    JS 省市区三级联动: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  5. 使用python生成c文件模板

    目标 完成一个python脚本,实现指定名字后,自动生成.c和.h的模板.例如: /** * @file epc.c * @author 陈维 * @version V01 * @date 2017. ...

  6. MySQL InnoDB类型数据库的恢复

     MySQL的数据库文件直接复制便可以使用,但是那是指“MyISAM”类型的表. 而使用MySQL-Front直接创建表,默认是“InnoDB”类型,这种类型的一个表在磁盘上只对应一个“*.frm”文 ...

  7. linux-shell脚本命令之awk

    [ awk简单介绍: ] awk能够从一个文本中获取部分内容, 或者对这个文本进行排版, 使它按某种格式输出. [ awk工作流程: ] awk会把文件一行内容去到内存里, 然后对这行内容进行分段 ( ...

  8. list if else 遍历 特征合并

    特征合并 import re l = ['a', 'b1'] ll = [i if re.search('\d', i) is None else i[0:re.search('\d', i).end ...

  9. 在VS2010下使用AppFace

    AppFace的介绍网上一大堆,此文仅为自己作个记录,方便以后查看. 一.需要的文件:1.AppFace.h  2.appface.lib 3.appface.dll 4.macosx_af.urf ...

  10. [RK3288][Android6.0] 关于uboot中logo相关知识点小结【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/76256224 Platform: Rockchip OS: Android 6.0 Kern ...