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. apache目录及文件讲解

    apache目录下bin,conf,htdocs,logs,modules讲解    bin:        ab  压力测试工具        apachectl  启动命令        apxs ...

  2. HDFS的Java客户端操作代码(HDFS删除文件或目录)

    1.HDFS删除文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.hadoo ...

  3. Python实战:Python爬虫学习教程,获取电影排行榜

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  4. 什么时候用using (SPSite site = new SPSite(SPContext.Current.Web.Url))

      并不是所有时候都适合用using(){},只有当需要提升用户的权限的时候才会用到using,其他时候都可以直接使用SPContext.Current.Web;             using ...

  5. Devexpress 使用经验 —— ASPxGridView前后台交互写法推荐

    这里的格式是仁者见仁智者见智,这篇随笔只是我在工作过程中总结出的阅读性高,对我来说效率较高的写法. ASPX: <dx:ASPxGridView ID="ASPxGridViewLin ...

  6. [DEncrypt] C# DEncrypt加密/解密帮助类(转载)

    点击下载 DEncrypt.rar 这个类是关于加密,解密的操作,文件的一些高级操作1.使用 缺省密钥字符串 加密/解密string2.使用 给定密钥字符串 加密/解密string3.使用 缺省密钥字 ...

  7. Struts2中EL表达式取值

    http://blog.csdn.net/cuihaiyang/article/details/41950141 (写的不错,可以知道为什么struts2可以用El取属性值的问题.正常el从reque ...

  8. ES6学习笔记之Promise

    入职百度EFE团队实习已经三周了,实习中接触到了生产环境的技术和开发流程,大开眼界,和自己在学校接小作坊式项目是很不一样的体验.其中一个很大的感触是,ES6早已不是“选修”的尝鲜技术,而是已经全面普及 ...

  9. JavaScript中Ajax的get和post请求

    AJAX = 异步 JavaScript和XML(Asynchronous JavaScript and XML) 作用:在不重新加载整个网页的情况下,对网页的某部分进行更新.   两种请求方式: 1 ...

  10. MySQL 时间戳(Timestamp)函数

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() mysql> select current_timestamp, curren ...