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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

代码如下:

public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String>list=new ArrayList<String>();
int len=nums.length;
if(len==0) return list;
int begin=nums[0];
int end=nums[0];
for(int i=0;i<len;i++){
begin=nums[i];
while(i+1<len && nums[i+1]-nums[i]==1)
i++;
end=nums[i];
String s=null;
if(begin!=end)
s=""+begin+"->"+end;
else
s=""+begin;
list.add(s); }
return list;
}
}

  运行结果:

(easy)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. C#解leetcode 228. Summary Ranges Easy

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

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

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

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

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

  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. css之padding,marging

    padding:内边距,所有浏览器都支持,不允许使用负值 继承内部格式生成了10px的边距. 属性: auto:浏览器计算机内边距. length:规定以具体单位计的内边距值,比如像素.厘米等.默认值 ...

  2. MySQL下载、安装和修改root密码

    一.下载地址:MySQL_5.6.22_winx64_XiaZaiBa :http://rj.baidu.com/soft/detail/12585.html?ald 二.安装软件,安装到指定的路径, ...

  3. nginx linux 下开机自动启动

    这里使用的是编写shell脚本的方式来处理 vi /etc/init.d/nginx  (输入下面的代码) #!/bin/bash# nginx Startup script for the Ngin ...

  4. Linux 下增大tomcat内存

    我的服务器的配置: # OS specific support.  $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...

  5. R(六): RODBC 访问SqlServer

    在我的实际工作中,数据来源一方面是关系型数据库MS SqlServer, 别一方面是HBase.本节主要介绍通过RODBC访问MS SqlServer 安装配置,参见资料(https://msdn.m ...

  6. ActionScript ArrayCollection sort

    var sortByOrderId:Sort = new Sort; sortByOrderId.fields = [new SortField("orderId")]; orde ...

  7. 通过FTP自动上传当天的备份数据

    @echo off del f:\ftpcfg.txt echo open 192.168.123.2>f:\ftpcfg.txt echo WMS>>f:\ftpcfg.txt e ...

  8. sql语句延时执行或者是指定时间执行

    --使用waitfor语句延迟或暂停程序的执行 --waitfor{delay'time'|time 'time'} delay是指间隔时间 最长到24小时 time是指定时间执行 waitfor d ...

  9. 黄聪:JS实现复制到剪贴板功能,兼容所有浏览器(转)

    两天前听了一个H5的分享,会议上有一句话,非常有感触:不是你不能,而是你对自己的要求太低.很简单的一句话,相信很多事情不是大家做不到,真的是对自己的要求太低,如果对自己要求多一点,那么你取得的进步可能 ...

  10. DISPOSE_ON_CLOSE 和 EXIT_ON_CLOSE 的区别

    If you have several JFrames open and you close one that has EXIT_ON_CLOSE it will close all the JFra ...