1 题目

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

接口:public List<String> summaryRanges(int[] nums);

2 思路

给定一个排序好的数组,无重复的元素,返回这个数组的范围。看示例容易理解。

顺序扫描一遍数组,用2个指针 startend来记录每一小段的范围,分割点是nums[end + 1] == nums[end] + 1),满足此条件,end指针往后移动。

细节:处理好数组的最后一个元素:若到了最后一个元素,不进行判断nums[end + 1] == nums[end] + 1),直接进入范围的统计处理。

复杂度: Time O(N); Space: O(1)

3 代码

       public List<String> summaryRanges(int[] nums) {
final int len = nums.length;
List<String> result = new LinkedList<String>();
for (int start = 0, end = 0; end < len;) {
if ((end + 1 < len) && (nums[end + 1] == nums[end] + 1)) {
end++;
} else {
if (start == end) {
result.add(Integer.toString(nums[start]));
} else {
result.add(nums[start] + "->" + nums[end]);
}
end++;
start = end;
}
}
return result;
}

4 总结

题目简单,思路清晰,注意代码的细节。

5 扩展

如果数组中有重复的元素,又该如何做?

  • 多添加一个判断:nums[end + 1] == nums[end] + 1 或者 nums[end + 1] == nums[end],满足条件,end指针往后移动一位。

6 参考

leetcode面试准备:Summary Ranges的更多相关文章

  1. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  2. 【刷题-LeetCode】228. Summary Ranges

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

  3. 【LeetCode】228 - Summary Ranges

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

  4. LeetCode OJ:Summary Ranges(概括区间)

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

  5. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

  6. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  7. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  8. leetcode面试准备: Game of Life

    leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...

  9. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

随机推荐

  1. git 远程追踪

    $ git branch --set-upstream-to origin/master http://stackoverflow.com/questions/21729560/how-to-make ...

  2. 使用httpModule做权限系统

    页面请求过程: 根据这个流程,网上一般的权限验证在:Http.Module.AuthorizeRequestHttp.Module.PreRequestHandlerExecute 例如使用前者: u ...

  3. C#磁盘遍历——递归

    static void Main(string[] args) { //创建秒表,记录查询的总时间 Stopwatch timer = new Stopwatch(); timer.Start(); ...

  4. 用Filezilla往ubuntu虚拟机上传文件

    也许不用这么复杂,但就这么干了 1.安卓ubuntu虚拟机 2.虚拟机安装ssh服务:sudo apt-get openssh-server 3.虚拟机新建目录test 4.修改test文件夹的访问权 ...

  5. Quartz.net 定时任务在IIS中未按时执行

    IIS 垃圾回收机制下解决Quartz.net 的不执行问题 IIS中涉及了垃圾回收机制,quartz.net 在ASP.NET 项目中可以实现线程监控定时执行任务,但是在IIS7.5机一下版本中涉及 ...

  6. Python编写相关注意事项

    1.# -*- coding: utf-8 -*-代码首部添加这个,不然会报Non_ASCII charater错误 python闭包:实际应用场景1.保持闭包运行完后的环境: 2.根据外部作用域的局 ...

  7. java.lang.InstantiationError: sun.net.ftp.FtpClient

    发送邮件功能.本地可以,测试环境上报错.是JDK  版本导致的,,本地1.6   测试环境JDK  1.7 解决办法: 1.测试环境重新配置jdk  1.6 环境.. 2.安装  JDK  1.7  ...

  8. oracle redo日志维护

    环境 OS:Red Hat Linux As 5 DB:10.2.0.1 1.添加日志组 alter database add logfile group 4 ('/u01/app/oracle/or ...

  9. [Twisted] deferred

    Twisted提供一个优雅的实现(Deferred)来管理回调函数. Deferred Object 的结构 Deferred Object包含两个回调函数列表.一个用来保存成功的回调函数,另一个用来 ...

  10. ios专题 - APP设计流程

    网上看到这篇文章,觉得基本的flow很有帮助,转过来收藏了:作者:关于Sarah Parmenter英国艾塞克斯(英国英格兰东南部的郡)Youknowwho设计工作室的创始人,Sarah Parmen ...