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

Example 1:

Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]

Example 2:

Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]

题目标签:Array

  题目给了我们一个nums array, 让我们找出它的区间总结。

  这题可以用two pointers 来做, 设一个left 和 right 起初都等于0。遍历nums array,如果遇到了 当前数字 等于 前一个数字+1, 那么就把right = i,扩大这个sliding window 的范围;如果遇到的 当前数字 不等于 前一个数字+1,意味着此时需要把前面的区间left 到 right加入list,因为这里已经断点了。而且还需要更新left 和 right 都等于 目前的 i, 让sliding window 重新开始。

  还需要注意的是,遍历完nums 之后,还需要把最后的区间 加入 list。

Java Solution:

Runtime beats 51.99%

完成日期:09/06/2017

关键词:Array, Two Pointers

关键点:利用left 和 right 来控制sliding window 的大小(区间)

 class Solution
{
public List<String> summaryRanges(int[] nums)
{
List<String> res = new ArrayList<>(); if(nums == null || nums.length == 0)
return res; int left = 0, right = 0; for(int i=1; i<nums.length; i++)
{
// if current number is not equal to previous number + 1, add the range into list
if(nums[i] != nums[i-1] + 1)
{
// if just one number
if(left == right)
res.add("" + nums[left]);
else// if more than one number
res.add("" + nums[left] + "->" + nums[right]); // if find the gap, update the left and right
left = i;
right = i;
}
else if(nums[i] == nums[i-1] + 1) // if find the correct number, increase right
right = i; } // add the last range
if(left == right)
res.add("" + nums[left]);
else // if more than one number
res.add("" + nums[left] + "->" + nums[right]); return res;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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

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

  3. C#解leetcode 228. Summary Ranges Easy

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

  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. (easy)LeetCode 228.Summary Ranges

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

  7. Java [Leetcode 228]Summary Ranges

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

  8. 228 Summary Ranges 汇总区间

    给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...

  9. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

随机推荐

  1. JS中有关分支结构、循环结构以及函数应用的一些简单练习

    案例一:搬桌子    年龄大于七岁男女都可以搬桌子,年龄小于七岁大于五岁的男生可以搬桌子: var num =parseInt(prompt("请输入你的年龄")) var sex ...

  2. [js高手之路]Node.js+jade抓取博客所有文章生成静态html文件

    这个周末,恶补了一下jade模板引擎,就为生成静态html文件,这篇文章需要知道jade以及看过我的上篇文章,我先给出他们的参考链接: [js高手之路]Node.js模板引擎教程-jade速学与实战1 ...

  3. Modeling -> Mixamo auto rigging -> UE4 retargeting

    In general, there are 3 ways we can work with the Blender-UE4 pipeline:   1 Model character Export m ...

  4. python只re模块

    while True: phone_number=input('please input your phone number:') if len(phone_number)==11 \ and pho ...

  5. Rigidbody(刚体) and Collider(碰撞器)

    关于刚体Rigidbody,手册上是这么描述的: Control of an object's position through physics simulation. 通过物理模拟控制一个物体的位置 ...

  6. 如何写好git commit message

    1.触发事件 我有这样一个版本库,里面包含两个学习用的练习项目:BookStore(以下简称BS)和PictureFriend(以下简称PF) 我在更改PF以后,未进行提交,同时又到BS中优化了一下文 ...

  7. Eclipse-远程调试服务器代码

    1, 将应用部署到服务器上可以正常启动 2, 在Eclipse中,有相同的项目(程序) 3, 项目右键(ops-web)-->Debug as --> Debug Configuratio ...

  8. 一个非常好用的框架-AngularJS(一)

      前  言           AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.An ...

  9. GitHub使用(一) - 新建个人网站

    1.首先进入“仓库Repositories”,点击“新建New”.

  10. 一个基于Asp.net MVC的博客类网站开源了!

    背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少心思,希望对你有所帮助. github地址:https: ...