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

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.

题意:

给定一个数组,统计其中元素的区间分布。

思路:

scan给定数组

若当前数字 = 前一个数字 + 1 ,则表示continuous range

若当前数字 != 前一个数字 + 1, 则表示不是continuous range, 即需要wrap前面的部分。

留心当for循环scan给定数组完毕,wrap了前面的部分,还需要把剩下的start 到 a.length-1 的部分加到result里

代码:

 class Solution {
public List<String> summaryRanges(int[] a) {
List<String> result = new ArrayList<>(); if(a.length==0){return result;} int start = a[0];
for(int i = 1; i < a.length; i++){
if(a[i] != a[i-1] +1){ // 数字不相连,则wrap前面的部分
group (result, start, a[i-1]);
start = a[i];
}
}
group (result, start, a[a.length-1]);// 不要忘记for循环之后剩下的一组
return result;
} private void group (List<String>list, int start, int end){
if(start == end){
list.add(start+"");
}else{
list.add(start+"->"+end);
}
}
}

[leetcode]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. LeetCode 228. Summary Ranges (总结区间)

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

  3. C#解leetcode 228. Summary Ranges Easy

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

  4. Java for LeetCode 228 Summary Ranges

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

  5. LeetCode(228) Summary Ranges

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

  6. (easy)LeetCode 228.Summary Ranges

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

  7. Java [Leetcode 228]Summary Ranges

    题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...

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

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

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

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

随机推荐

  1. python3 tkinter

    https://morvanzhou.github.io/tutorials/python-basic/tkinter/ 李导师推荐的.非常感谢.非常棒的视频教程!!! 只不过里面的教程视频是yout ...

  2. Server Tomcat v8.0 Server at localhost failed to start.的解决方法

    1.可能是web.xml中的filter-mapping中url-pattern没加/* 2.可能是servlet和servlet-mapping中的servlet-name不匹配

  3. tensorflow定义神经网络损失函数MSE

    import numpy as np import tensorflow as tf y_pred = np.array([[1], [2], [3]],dtype=np.float32) y_rea ...

  4. Maven web项目启动出错

    问题描述: 在第一次建立maven项目后,启动测试,结果jsp页面报告404错误 问题解决: 在pom.xml中引入servlet-api 和 jsp-api,maven不会自动的添加,因此,要在首次 ...

  5. python之路day03

    1  复习计算机基础 计算机基础我们讲到完整的计算机系统包括了:应用程序,操作系统,硬件三部分.那么硬件又分为:cpu,内,和硬盘. 对于用户来说我们操作计算机是通过应用程序来间接控制计算机.当我们打 ...

  6. MySQL数据库Innodb储存引擎----储存页的结构

    上一篇博客回顾: 1:数据库拥有众多的储存引擎,现在主要使用的是Inoodb,这个储存引擎有Compact,Redundant,Dynamic,Compressed四种行格式 2:Compact行格式 ...

  7. 在IDEA下使用Spring Boot的热加载(Hotswap)

    你是否遇到过这样的困扰: 当你写完一段代码后,要看到效果,必须点击IDEA的停止按钮,然后再次重启启动项目,你是否觉得这样很烦呢? 如果你觉得很烦,本文就是用来解决你的问题的. 所谓热加载,就是让我们 ...

  8. npm WARN react-native-maps@0.14.0 requires a peer of react@>=15.4.0 but none was installed

    install  the  react-native     here comes a  questions :: npm WARN react-native@0.41.2 requires a pe ...

  9. 网络层-IP地址

    以下内容是IPv4 IP地址长度32位,Java里面一个int的长度,总共分为5类IP地址 1:分类编址 A类IP地址0开头:           A类有31个位置可以变化,总数是2^31个, [(0 ...

  10. 网页向flash传参数。显示视频。(例子)

    [例子1]网页向flash传参数,显示视频: 下面要做的事情:做一个flash文件,可以通过网页得到参数(视频文件名).然后显示视频,并在文本框中显示视频文件名的文字. 1.建立一个flash文件:3 ...