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. vs2012 Nuget错误:“未能解析此远程名称api.nuget.org”

    今天在使用vs2012的Nuget功能向新建MVC项目中添加bootstrap时出现一个错误:“未能解析此远程名称api.nuget.org”: 网上查阅资料得知:nuget.org使用的godadd ...

  2. N天前涨停过的票

    zd(0,300,10)B2:=REF(C,1)*1.1-C;B3:BARSLAST(B2<0.01)=ZD01;{n天前涨停股票}

  3. hadoop-2.7.0

    65 cd /home/guyumei/下载/ 66 ll 67 cd .. 68 ll 69 cd .. 70 ll 71 cd guyumei/ 72 ll 73 cd hadoop-2.7.0/ ...

  4. maven时候Embedded error: error in opening zip file

    maven时候Embedded error: error in opening zip file 用 mvn clean install -Dmaven.test.skip=true -Denv=re ...

  5. 利用Java Service Wrapper将java项目添加到windows服务中

    1.web项目,即tomcat/resin添加至window系统服务,步骤如下:第一步:找到tomcat的bin目录,如:D:\apache-tomcat-8.0.26\bin第二步:打开cmd,cd ...

  6. Android 查看內存使用

    一.使用dumpsys meminfo命令 1.使用dumpsys meminfo查看内存使用情况 2.过滤某个进程可以使用 dumpsys meminfo | grep -i phone 二,使用t ...

  7. jquery组件团购倒计时功能

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. SQL中的CASE的用法

    CASE在SQL语句中,很有点类似java等高级编程语言中的switch这样子的多分枝语句,但是有点不同的是,case后面接的是when,另外,when的后续分枝有点类似if后面接else.这个是我的 ...

  9. Linux dirname $0 source if

    $SHELL gives the full path to your default shell. $0 gives the name of your current shell. dirname是一 ...

  10. Hibernate3回顾-6-hibernate缓存(性能优化策略)

    主要来源: http://blog.csdn.net/csh624366188/article/details/7612142  (比较详细) http://www.cnblogs.com/20091 ...