LeetCode Missing Ranges
原题链接在这里:https://leetcode.com/problems/missing-ranges/
题目:
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
Example:
Input: nums =[0, 1, 3, 50, 75], lower = 0 and upper = 99,
Output:["2", "4->49", "51->74", "76->99"]
题解:
If nums is null or empty, just add getRange(lower, upper).
Otherwise, first add range between lower and nums[0].
Then add missing range within nums.
Last, add range between nums[nums.length - 1] and upper.
Time Complexity: O(n). n = nums.length.
Space: O(1). regardless res.
AC Java:
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> res = new ArrayList<>();
if(nums == null || nums.length == 0){
res.add(getRange(lower, upper));
return res;
}
if(lower < nums[0]){
res.add(getRange(lower, nums[0] - 1));
}
for(int i = 1; i<nums.length; i++){
if(nums[i - 1] != nums[i] && nums[i - 1] + 1 != nums[i]){
res.add(getRange(nums[i - 1] + 1, nums[i] - 1));
}
}
if(nums[nums.length - 1] < upper){
res.add(getRange(nums[nums.length - 1] + 1, upper));
}
return res;
}
private String getRange(int l, int r){
if(l == r){
return "" + l;
}
return l + "->" + r;
}
}
类似Summary Ranges, Data Stream as Disjoint Intervals.
LeetCode Missing Ranges的更多相关文章
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [leetcode]163. Missing Ranges缺失范围
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...
- 【LeetCode】Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- Missing Ranges -- LeetCode
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its mi ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
随机推荐
- Air Raid[HDU1151]
Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 【POJ3237】Tree 树链剖分+线段树
[POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...
- odeforces Beta Round #77 (Div. 2 Only)
A. Football time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 在Linux中安装Tomcat
带Linux的虚拟机中安装Tomcat 一.从官方网站上下载tomcat软件包.http://tomcat.apache.org/ apache-tomcat-7.0.33.tar.gz 二.下载到本 ...
- JS 弹出模态窗口解决方案
最近在项目中使用弹出模态窗口,功能要求: (1)模态窗口选择项目 (2)支持选择返回事件处理 在IE中有showModalDialog 方法,可以很好的解决该问题,但是在Chrome中和FF中就有问题 ...
- 用css3实现一个带缺口的圆圈(图)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js变量
由于undefined和null两个值的比较是相等的,所以,未初始化的变量和赋值为null的变量会相等.这时,可以采用typeof变量的类型进行比较.但,建议还是养成编码的规范,不要忘记初始化变量. ...
- POJ 3321 树状数组(+dfs+重新建树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27092 Accepted: 8033 Descr ...
- PHP filesystem attack vectors
http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...
- 第 2 章 让jsp说hello
2.1. 另一个简单jsp 上一篇举的例子很单纯,无论谁向服务器发送请求,服务器都只计算当前系统时间,然后把这个时间制作成http响应发还给浏览器. 可惜这种单向的响应没办法实现复杂的业务,比如像这样 ...