Description:

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

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

确定有序数组的区间

需要注意各种边界细节,每次确定区间之后需要清空。

public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<String>();
if(nums.length == 0)
return list;
if(nums.length == 1) {
list.add(nums[0]+"");
return list;
}
boolean flag = false;
StringBuilder ele = new StringBuilder();
for(int i=0; i<nums.length; i++) {
int start = 0, end = 0; if(!flag) {
start = nums[i];
flag = true;
ele.append(start+"");
} if(i<nums.length-1 && nums[i] != nums[i+1]-1) {
end = nums[i];
flag = false;
if(nums[i] != start) {
ele.append("->" + end);
}
} if(ele != null) {
if(i == nums.length-2 && nums[i] == nums[i+1]-1) {
end = nums[i+1];
ele.append("->" + end);
list.add(ele.toString());
ele.delete(0, ele.length());
break;
}
if(!flag || flag&&i==nums.length-1) {
list.add(ele.toString());
ele.delete(0, ele.length());
}
}
}
return list;
}
}

LeetCode——Summary Ranges的更多相关文章

  1. [LeetCode] Summary Ranges 总结区间

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

  2. (leetcode)Summary Ranges

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

  3. 228. [LeetCode] Summary Ranges

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

  4. LeetCode Summary Ranges (统计有序数列范围)

    题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...

  5. leetcode面试准备:Summary Ranges

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

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

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

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

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

  8. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

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

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

随机推荐

  1. 用STS创建Maven的Web项目<转>

    右键New——>other——>Maven——>Maven Project 弹出框中点击Next,在Filter中写上:webapp. 然后在下面的框中选择org.apache.ma ...

  2. dac7562 应用层实现dac

    /* * dac7562 (using spidev driver) *  */ #include <stdint.h>#include <unistd.h>#include ...

  3. openwrt使用list

    openwrt中用到双向无头链表,实际应用时应在外部定义实体链表头,后续可直接应用链表函数(宏定义已将链表头排除在外): static struct list_head timeouts = LIST ...

  4. 官网下载到离线的Adobe Acrobat Reader DC

    Adobe 官方 FTP :ftp://ftp.adobe.com/ Adobe Acrobat Reader DC 下载目录:ftp://ftp.adobe.com/pub/adobe/reader ...

  5. Lua语言开发Cocos2d-x游戏视频教程第L0401课-Cocos2d-x中使用Lua

    http://www.eoeandroid.com/thread-320733-1-1.html

  6. EcmaScript对象克隆之谜

    先谈谈深拷贝 如何在js中获得一个克隆对象,可以说是喜闻乐见的话题了.相信大家都了解引用类型与基本类型,也都知道有种叫做深拷贝的东西,传说深拷贝可以获得一个克隆对象!那么像我这样的萌新自然就去学习了一 ...

  7. Spark 基础及RDD基本操作

    什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合.RDD具有数据 ...

  8. Python:基本运算、基本函数(包括复数)、Math模块、NumPy模块

    基本运算 x**2 : x^2 若x是mat矩阵,那就表示x内每个元素求平方 inf:表示正无穷 逻辑运算符:and,or,not 字典的get方法 a.get(k,d) 1 1 get相当于一条if ...

  9. win7语音识别开发(sapi)

    参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx    (sapi5.4 reference) http://msdn ...

  10. Linux Vi/Vim 的使用及实例

    什么是 vim? Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. 简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但是 ...