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. 利用Vim提供的正则去掉代码每行开头不想要的行号以及vi常见问题和应用技巧

    看到一不错的代码片断,但奈何无论怎么拷贝,粘贴到VS里都是带行号的,300多行的代码手工删除行号与前面的空格可得耗不少时间...这时想起了无所不能的VIM,以及它的正则,它的替换功能.解决步骤: 1. ...

  2. 纯CSS写正方形自适应宽高,且左侧高与正方形高保持一致

    先上图 虽然写法有点暴力,但是效果还是可以的,哈哈哈哈哈html:<div class="box">            <div class="le ...

  3. python基础之七种运算符

    废话不多说,上节说的是数据类型,本篇讲讲数据运算. 在算式"1+2"中,"1"和"2"被称为操作数,"+"被称为运算符 ...

  4. Dom4J生成xml和包含CDATA问题

    在 java注解生成xml和包含CDATA问题里面做了介绍,这里直接贴代码. 1:生成xml的java文件 package com.dufy.test.xml; import java.io.File ...

  5. Relocation 状态压缩DP

     Relocation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. 记录一下从懵懂到理解RESTful的过程

    前言 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)R ...

  7. Thirft框架快速入门

    Thrift介绍1.什么是thrift?thrift早期由facebook内部团队开发,主要用于实现跨语言间的方法调用,属于远程方法调用的一种,后开源纳入apache中,成为了apache thrif ...

  8. Ubuntu 普通用户提升到root权限

    方法一.修改passwd文件 1.编辑passwd文件 sudo vim /etc/passwd 2.找到你想提权的用户(比如test),将用户名后面的数字改成0 找到用户test test:x::: ...

  9. ASP.NET没有魔法——开篇-用VS创建一个ASP.NET Web程序

    为什么写这一系列文章? 本系列文章基于ASP.NET MVC,在ASP.NET Core已经发布2.0版本,微服务漫天的今天为什么还写ASP.NET?. 答:虽然现在已经有ASP.NET Core并且 ...

  10. wpf 中英文版编写

    var uriC = new Uri("/YTManage.Language;component/Chinese.xaml", UriKind.Relative); // 得到资源 ...