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. php7+apache2.4 安装(window)

    一.下载 需要下载 php7.apache2.4.vc2015(VC库) PHP7下载地址:http://windows.php.net/download/ Apache 下载地址:https://h ...

  2. oracle 字符集 AL32UTF8、UTF8

    简介:ORACLE数据库字符集,即Oracle全球化支持(Globalization Support),或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数据.利用全球化支持,ORA ...

  3. POJ 1523 SPF 割点与桥的推断算法-Tarjan

    题目链接: POJ1523 题意: 问一个连通的网络中有多少个关节点,这些关节点分别能把网络分成几部分 题解: Tarjan 算法模板题 顺序遍历整个图,能够得到一棵生成树: 树边:可理解为在DFS过 ...

  4. HTTP协议详解(文档)

    目录引言................................................................................................ ...

  5. 【转】JS函数集合大全

    1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...

  6. Deep Learning 经典网路回顾#之LeNet、AlexNet、GoogLeNet、VGG、ResNet

    #Deep Learning回顾#之LeNet.AlexNet.GoogLeNet.VGG.ResNet 深入浅出——网络模型中Inception的作用与结构全解析 图像识别中的深度残差学习(Deep ...

  7. 使用Grunt构建任务管理脚本(转)

    Grunt是构建Web开发的一个系统,但它创建比较困难.在这个指南中,你将学会如何配置Grunt创建一个现代的Web项目.当你完成教程中的配置之后,你的Gruntfile将具有: 从源目录中向目标目录 ...

  8. CSS导入使用及引用的两种方法

    方法一<link rel="stylesheet" type="text/css" href="test.css"> 方法二&l ...

  9. CentOS6.8安装mysql并设置字符集编码

    一.安装: 1. 首先以root用户进入系统 2. 输入命令: yum install mysql mysql-server -y 等待安装完成. 3. 启动MySQL,输入命令: /etc/init ...

  10. Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件

    SwipeRefreshLayout在v4包下,相应的v4Demo中也有相应的样例.假设没有请下载最新support-v4 SwipeRefreshLayout 仅仅能有一个直接子View,可能是一个 ...