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. Linux性能分析 vmstat基本语法

    vmstat      vmstat 统计虚拟内存信息,可以对操作系统的proc.memory.CPU.IO等信息进行统计以呈现给用户.   根据操作系统的不同,vmstat的输出结果会有不同.大家可 ...

  2. python中获取当前路径并添加到系统路径

    import os import sys sys.path.append(os.getcwd())

  3. How to Use vcpkg On Windows

    Introduction If you do any sort of C++ development on Windows, then you know that library/package ma ...

  4. 【C++11新特性】 - 空间配置allocator类

    原文链接: http://blog.csdn.net/Xiejingfa/article/details/50955295 今天我们来讲讲C++的allocator类. C++提供了new和delet ...

  5. Dividing Infinity - Distributed Partitioning Schemes

    This is the second post in a series discussing the architecture and implementation of massively para ...

  6. 链接mysql的两种方法

    使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@host]# my ...

  7. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  8. noip 2011 选择客栈

    题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...

  9. Oracle 存储过程 延迟执行 DBMS_LOCK.SLEEP(60);

      --测试代码: declare -- Local variables here i integer; begin -- Test statements here dbms_output.put_l ...

  10. selenium+python自动化85-python3.6上SendKeys报错用PyUserInput取代

    前言 python2上安装SendKeys库,对于不好定位的元素,用快捷键操作是极好的,那么在3.6上安装时,会报错 python3.6安装SendKeys报错 1.python3.6安装SendKe ...