leetcode面试准备:Summary Ranges
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个指针 start 和 end来记录每一小段的范围,分割点是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的更多相关文章
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【刷题-LeetCode】228. Summary Ranges
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- 【LeetCode】228 - Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode OJ:Summary Ranges(概括区间)
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- Missing Ranges & Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
随机推荐
- NDK开发之调用方法
与NDK开发之访问域中介绍的一样,Java中的方法也是分为两类:实例方法和静态方法.JNI提供了访问两类方法的函数,下面我们一起来看看怎么在C中访问Java中的方法. 我们的MainActivity中 ...
- winows 进程通信的实例详解
发送端: 新建一个基本对话框工程,添加6个文本框控件,并且关联控件变量(CString类型): m_strCopyData, m_strFileMap, m_strMem, m_strRegMsg, ...
- centos could not retrieve mirrorlist
centos could not retrieve mirrorlist >>>>>>>>>>>>>>>> ...
- Java-hibernate的Hello World
hibernate 是对jdbc进行轻量级封装的 orm 框架,充当项目的持久层. 要使用 hibernate首先就需要继续配置, 引包:下载hibernate然后加入jar包 同时引入mysql ...
- DLL Export 报错
编译报错: error : syntax error at token xxxx 修改非Unicode 系统区域设计即可
- Context是什么,怎么用
一.Context是什么 开始学安卓的时候发现经常有context,但是都不知道为什么,什么时候需要它. 官方文档概述:关于应用程序环境的全局信息的接口.这是一个抽象类,它的实现是由安卓系统提供的.它 ...
- Update Statistics用法
Update Statistics语句的作用将创建的数据库表的有关统计信息更新到系统 sysmater的相关表中,以便查询优化器选择最佳的执行路径,当sysmaster库中没有相应的统计信息,或者统计 ...
- SQL通过传递参数方式备份数据库.
存储过程的SQL代码: ALTER PROCEDURE USP_DBBackup ), --存储目录. ) --存储数据库名. AS SET NOCOUNT ON ) select @name = r ...
- C#动态二维数组输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- CSS 分组 和 嵌套 选择器
Grouping Selectors 在样式表中有很多具有相同样式的元素. h1{color:green;}h2{color:green;}p{color:green;} 为了尽量减少代码,你可以使用 ...