原题链接在这里: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的更多相关文章

  1. [LeetCode] Missing Ranges 缺失区间

    Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...

  2. LeetCode 163. Missing Ranges (缺失的区间)$

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

  3. [leetcode]163. Missing Ranges缺失范围

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  4. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  5. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

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

  6. [LeetCode#163] Missing Ranges

    Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...

  7. 【LeetCode】Missing Ranges

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

  8. Missing Ranges -- LeetCode

    Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its mi ...

  9. 【LeetCode】163. Missing Ranges 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

随机推荐

  1. linux 安装vbox增强工具

    首先在虚拟机控制台点设备--------安装增强功能,这样会用虚拟光驱加载增强功能镜象. 然后打开终端,先转到root身份:=================su================= f ...

  2. Codeforces Round #353 (Div. 2) A. Infinite Sequence

    Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...

  3. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  4. 一个动画 Label (走马观花)

    UILabel中一个水平移动的Label UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 300)]; U ...

  5. IP地址分类整理

    什么是IP地址? IP地址就是计算机在网络中地址. IP地址有多少个? IP地址范围是:0.0.0.0~225.225.225.255,这只是人为了方便记录才转为十进制的,ip地址实际是一个32位地址 ...

  6. 4分钟了解nano编辑器和简单命令 2015.10.6

    nano感觉并不常用,但是偶尔遇到过几次. nano命令是一个类似VI的编辑器,但是更简单,其中的i,a等命令似乎可以用,但是有些命令不可以用.保存和退出最大的区别在于退出方式,如果你要保存所做的修改 ...

  7. 【BZOJ】3065: 带插入区间K小值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...

  8. Codeforces Beta Round #2

    A题,神题意题.. #include <iostream> #include <cstdio> #include <cstring> #include <st ...

  9. ThinkPHP3.2.2 Widget扩展以及widget demo实例

    Widget扩展一般用于页面组件的扩展. 先说明Widget被调用的方法,你只需要在你的模板文件中使用这样的语法:{:W("Demo/demo_widget_method",arr ...

  10. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...