LeetCode 228. Summary Ranges (总结区间)
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 (总结区间)的更多相关文章
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- C#解leetcode 228. Summary Ranges Easy
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java for LeetCode 228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
随机推荐
- Fabric 1.0的多机部署
Fabric1.0已经正式发布一段时间了,官方给出的单机部署的脚本也很完备,基本上傻瓜式的一键部署,直接运行官方的network_setup.sh up即可.但是在实际生产环境,我们不可能把所有的节点 ...
- Linux SSH下安装Java并设置环境
我是用Xshell进行远程连接阿里云服务器的,所以jdk不好下载. 我使用的是Winscp远程软件,在window上下载了jdk然后再上传到Linux服务器上 下面是安装的步骤 1.下载jdk8 登录 ...
- MySQL binlog 的恢复操作
测试出有个问题:mysqlbinlog 不加任何参数 恢复整个binlog 日志文件发现里面有这个操作 SET @@SESSION.GTID_NEXT 的操作, 如果需要恢复文件的时候就需要把他过 ...
- 浅谈大数据和hadoop家族
按照时间的早晚从大数据出现之前的时代讲到现在.暂时按一个城市来比喻吧,反正Landscape的意思也大概是”风景“的意思. 早在大数据概念出现以前就存在了各种各样的关于数学.统计学.算法.编程语言的研 ...
- CKEditor与dotnetcore实现图片上传
CKEditor的使用 1.引入js库 <script src="https://cdn.ckeditor.com/4.6.1/standard-all/ckeditor.js&quo ...
- python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍"获得当前路径""获得 ...
- Grunt针对静态文件的压缩,版本控制打包方案
在讲之前先谈谈大致步骤:安装nodejs -> 全局安装grunt -> 项目创建package.json --> 项目安装grunt以及grunt插件 -> 配置Gruntf ...
- SSM之整合Redis
Redis安装与使用 第一步当然是安装Redis,这里以Windows上的安装为例. 首先下载Redis,可以选择msi或zip包安装方式 zip方式需打开cmd窗口,在解压后的目录下运行redis- ...
- QT_FORWARD_DECLARE_CLASS
相当于class 类名. 那么他和#include 包含头文件有什么区别呢 首先我们为什么要包括头文件问题的回答很简单通常是我们需要获得某个类型的定义(definition).那么接下来的问题 ...
- 计算机基础--Java中int char byte的关系
计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...