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

示例 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. Android TextView设置个别字体样式

    TextView进一步深化:       Textview 能够对其文字进行格式化.       通过查询资料,了解到格式化文字的方式主要分为两大类:  第一类:HTML标签格式化文字      代码 ...

  2. 【转】TestNG执行顺序控制

    1.class执行顺序控制---testng.xml之preserve-order preserve-order:用来控制<test>里面所有<classes>的执行顺序.&l ...

  3. bootstrap中固定table的表头

    前端时间鼓捣的一个简单的手机站点,今天又拿出来弄一弄 由于主要是给手机訪问.用的是bootstrap响应式布局,今天的任务是做一个数据展示页面 可是因为数据的列比較多.所以横向显示不下,为了较好的显示 ...

  4. Makefile详解 (转--不错就是有点长)

    概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的程序员,make ...

  5. Mac下Git项目使用的.gitignore文件

    https://www.gitignore.io/ 这个网站可以搜索特定项目.系统所需要的.gitignore 我现在主要是在Mac上用Visual Studio Code进行开发,所以直接搜索Mac ...

  6. Java对象的创建过程

    //TODO https://www.cnblogs.com/chenyangyao/p/5296807.html

  7. MVC+ZTree大数据异步树加载

    实例部分: 首先是为ZTree提供的数据规范,定义一个标准的接口,这样对于前台调用是清楚的,简单的,因为它返回的JSON数据将与ZTree默认的数据元素保持一致 /// <summary> ...

  8. 授权QQ登录的qq端前端页面变迁

    ac_type = 'qq' if ac_type == 'qq': myid, mypwd = qq_key xp = '/html/body/div/div/div[2]/div/div/div/ ...

  9. how to use datatables editor

    Basic initialisation Editor is a Create, Read, Update and Delete (CRUD) extension forDataTables that ...

  10. spring boot自定义properity

    1.spring boot使用application.properties默认了很多配置. 但有时需要自定义配置.若在application.properties添加属性: app.name=fish ...