题目描述:

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

解题思路:

相邻的两个数字若差距为1,则继续向后读,直到不满足条件为止,则把起始到终点的结果表示出来。如此,直到读到数组的最后位置为止。

代码如下:

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

  

Java [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 for LeetCode 228 Summary Ranges

    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. 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. hdu 3807

    很好的思路     枚举有多少人有ipad 判是否满足题目给出的条件 #include <iostream> #include <cstring> #include <c ...

  2. RFID开发笔记 Alien阅读器文档

    1. 开机使用serial connect,完成boot后使用TCP/IP协议与主机通信 2.TagList,是一个活跃标签的列表,这里活跃的含义是在一个间隔里被监听到.如果一个标签之前没有被监听到, ...

  3. Amazon 面经

    [版面:待字闺中][首篇作者:gmadj] , 2013年09月29日21:51:33 [首页] [上页][下页][末页] [分页:1 2 ] gmadj 进入未名形象秀 我的博客     [回复] ...

  4. 【hadoop2.6.0】一句话形容mapreduce

    网上看到的: We want to count all the books in the library. You count up shelf #1, I count up shelf #2. Th ...

  5. C Primer Plus 第3章 数据和C 编程练习

    1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...

  6. CKeditor 配置使用

    CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...

  7. 套题T6

    过节(festival.cpp/c/pas) Dxy帮老师们过教师节的时候需要购买礼物.货架上有n 种礼物,每种礼物有不同的个数.每种礼物有价值和花费两种属性,帮他算出最大可以得到的价值.M是带的钱数 ...

  8. [SharePoint 2013 入门教程 3 ] 排版第一个网站集,网站

    我们创建了一个TEST网站集,如果你觉得太丑,怎么办,我们一起来给它整整容吧. 点击页面--> 编辑页面 我们现在就可以在页面上添加各种部件,进行布局排版.

  9. [topcoder]BoxesDiv2

    https://community.topcoder.com/stat?c=problem_statement&pm=13192 #include <vector> #includ ...

  10. 张小龙谈如何写E-mail软件

    编者语:鼎鼎大名的Foxmail软件制作者,你一定不会陌生吧!本刊第三期特刊还刊登过此君的生活照一张,可谓威风八面.小编此次突发奇想,“死缠烂打”,费了九牛二虎之力,终于约他写了一篇有关写E-mail ...