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. oracle合并查询

    1). Union 该操作符用于取得两个结果集的并集.当使用该操作符时,会自动去掉结果集中重复行. 2).union all 该操作符与union相似,但是它不会取消重复行,而且不会排序. 3). I ...

  2. 字符串反转实现(C++)

    字符串反转 C++实现,不使用系统函数: // ReverseString.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include ...

  3. 【Android】数据库的简单应用——增删改查的操作

    还记得getReadableDatabase()和getWritableDatabase()方法吧?在调用它们的时候会返回一个SQLiteDatabase对象,借助这个对象就可以进行CURD(Crea ...

  4. spring security 11种过滤器介绍

    1.HttpSessionContextIntegrationFilter 位于过滤器顶端,第一个起作用的过滤器. 用途一,在执行其他过滤器之前,率先判断用户的session中是否已经存在一个Secu ...

  5. Spring Framework jar官方直接下载路径

    SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用Maven和Gradle开发项目的,下载就非常麻烦,下给出Spring Framework jar官方直接下载路径: h ...

  6. MyXLS案例

    using System; using System.Data; using org.in2bits.MyXls; namespace Maticsoft.Common { /// <summa ...

  7. LINQ数据库连接对象制造工厂

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  8. CSS3高性能动画

    CSS动画属性会触发整个页面的重排relayout.重绘repaint.重组recomposite Paint通常是其中最花费性能的,尽可能避免使用触发paint的CSS动画属性,在CSS动画中使用w ...

  9. 关于for循环中的闭包问题

    还是昨天的那个简单的小项目,已经花了一天的时间了 - - .从&&的用法,到CSStext,到今天马上要谈的闭包(closure),通过一个小东西,真真发现了自己的各方面不足.昨天发完 ...

  10. nau8822 codec driver 录音时mic bias 无法自动打开问题

    nau8822 codec driver 录音时mic bias 无法自动打开问题 问题描述: kernel版本:3.10 在nuc970上测试nau8822驱动时发现,虽然驱动中有如下定义: SND ...