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"].

解题思路:

JAVA实现如下:

public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<String>();
if (nums.length == 0)
return list;
int left = nums[0], index = nums[0];
for (int i = 1; i < nums.length; i++) {
if (index == nums[i] - 1)
index++;
else if (index == left) {
list.add(index + "");
left = nums[i];
index = nums[i];
} else {
list.add(left + "->" + index);
left = nums[i];
index = nums[i];
} }
if (index == left)
list.add(index + "");
else
list.add(left + "->" + index); return list;
}

Java for 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. Java [Leetcode 228]Summary Ranges

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

  3. LeetCode 228. Summary Ranges (总结区间)

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

  4. C#解leetcode 228. Summary Ranges Easy

    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. [leetcode]228. Summary Ranges区间统计

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

  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. jquery-ui 的 主题 选择什么颜色? 建议使用html5 的标准进行书写, 更简洁!

    jQuery ui有多种主体, 基本上, 不能使用 no theme 的"主题包" base: 是基本的, 颜色以深灰色为主, 高亮显示为蓝色, ui lightness(明快) ...

  2. Linux服务器管理: 日志管理(一)

    1.日志管理介绍: a.日志服务:在CentOS6.x中日志服务以及由rsyslogd取代了原有的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日子文件的格 ...

  3. Java反射机制(Reflection)

    Java反射机制(Reflection) 一.反射机制是什么 Java反射机制是程序在运行过程中,对于任意一个类都能够知道这个类的所有属性和方法;对于任意一个对象都能够调用它的任意一个方法和属性,这种 ...

  4. mysql update操作

    update语法 Single-table语法: UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=ex ...

  5. Google Protocol Buffer 简单介绍

    以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...

  6. hessian接口参数,子类与父类不能有同名字段解决方法

    hessian默认是使用 com.caucho.hessian.io.JavaSerializer 序列化,同名字段子类字段值被赋值两次,最终用父类null值赋给了子类同名字段,解决方法就是 指定序列 ...

  7. jquery选择器(三)-过滤选择器

    一.基本过滤选择器 二.内容过滤选择器 1. 包含文本内容为“text”的元素 2. 含有某个选择器所匹配的父元素 3. 包含有子元素或者文本的父元素 4. 不含有子元素或者文本的父元素 三.可见性过 ...

  8. Mac 上真正替换LiveWriter 的工具 - ecto

    Mac 上真正替换LiveWriter 的工具 - ecto 13年开始使用mac.而后想把 windows 替换到.一直在寻找LiveWriter 的工具,至今终于找到 我先感谢这位博主 http: ...

  9. Java Io(数据输入输出流)

    Java Io 字节流中的DataInputStream 和 DataOutputStream,使用流更加方便,是流的一个扩展,更方便读取int, long,字符等类型数据. 事例代码如下: pack ...

  10. navigationcontroller手势翻页和navigationbar

    一. 系统导航默认手势 #import "CBNavigationController.h" //手势返回 @interface CBNavigationController () ...