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 ...
随机推荐
- Could not execute JDBC batch update; SQL [delete from role where roleId=?]; constraint [null]; neste
今天在写多个删除功能的时候出现了这么一个错误:意思是删除操作的时候,没有找到对应的外键. Cannot delete or update a parent row: a foreign key con ...
- junit initializationError和找不到或无法加载主类
今天在做Junit测试的时候 出现了initialzationError , 在网上查找,一般都是因为Jar 包冲突或者缺少Jar包导致的, 但是我的其他方法是可以使用Junit 的, 所以感觉应该跟 ...
- Java开发规范总结(两周至少看一次)
Service / DAO 层方法命名规约: 1 ) 获取单个对象的方法用 get 做前缀.2 ) 获取多个对象的方法用 list 做前缀.3 ) 获取统计值的方法用 count 做前缀.4 ) 插 ...
- UVW源码漫谈(番外篇)—— Emitter
这两天天气凉了,苏州这边连续好几天都是淅淅沥沥的下着小雨,今天天气还稍微好点.前两天早上起来突然就感冒了,当天就用了一卷纸,好在年轻扛得住,第二天就跟没事人似的.在这里提醒大家一下,天气凉了,睡凉席的 ...
- 编号中的数学_KEY
题目描述: 从美国州际高速公路建筑者那里,奶牛们引进了一种路径编号系统,来给牧场之间的道 路编号.他们已经把 N(1<=N<=25)个牧场,用 1 到 N 的整数编号.现在他们需要将牧场间 ...
- [js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)
接着上文[js高手之路] html5 canvas系列教程 - 状态详解(save与restore),相信大家都应该玩过美颜功能,而我们今天要讲的就是canvas强大的像素处理能力,通过像素处理,实现 ...
- iOS9.3越狱
转载:http://bbs.feng.com/read-htm-tid-10680439.html 首先是Windows英文版越狱的的教程 下载 Cydia Impactor 工具(用来安装越狱A ...
- JSP入门 文件上传
commons-fileupload public void save(HttpServletRequest request,HttpServletResponse response) throws ...
- 分享基于分布式Http长连接框架
第一次在博客园写文章,长期以来只是潜水中.本着不只索取,而要奉献的精神,随笔文章之. 现贡献一套长连接的框架.如下特性: 1:发布者可异步发送消息,消息如果发送失败,可重试发送,重试次数基于配置,消息 ...
- python --- json模块和pickle模块详解
json:JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式(用于数据序列化和反序列化).(适用于多种编程语言,可以与其他编程语言做数据交换 ...