原题链接在这里:https://leetcode.com/problems/missing-ranges/

题目:

Given a sorted integer array nums, where the range of elements are in the inclusive range [lowerupper], 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 RangesData 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. BZOJ4117 : [Wf2015]Weather Report

    一种天气情况的概率只与4种天气的出现次数有关,故将相同概率的情况计数后放入堆中模拟哈夫曼树即可. 每次取出概率最小的,将它个数除以2,对于零头需要特判. #include<cstdio> ...

  2. Javascript中大括号“{}”的多义性

    摘要:本文主要介绍JavaScript中大括号有四种语义作用. JS中大括号有四种语义作用 语义1,组织复合语句,这是最常见的 if( condition ) { //... }else { //.. ...

  3. 基于webpack的前端工程化开发解决方案探索(一):动态生成HTML(转)

    1.什么是工程化开发 软件工程的工程化开发概念由来已久,但对于前端开发来说,我们没有像VS或者eclipse这样量身打造的IDE,因为在大多数人眼中,前端代码无需编译,因此只要一个浏览器来运行调试就行 ...

  4. Ubuntu Gnome 14.04.2 lts 折腾笔记

    unity感觉不爽,于是来折腾gnome3 = = 首先去官网下载ubuntu gnome 14.04.2 lts的包(种子:http://cdimage.ubuntu.com/ubuntu-gnom ...

  5. Js实现MD5加密

    在页面中引用md5.js文件,调用方法为 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8&qu ...

  6. 1069. The Black Hole of Numbers (20)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  7. [LintCode] Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  8. mysql查询缓存参数

    由人说mysql查询缓存是鸡肋,也许吧,但还是要看场景: 查询缓存: 开启查询缓存:/etc/my.cnfquery_cache_type=1 重启

  9. 调试WEB APP多设备浏览器

    方法:adobe shadow  \ opera远程调试\ weinre adobe shadow: 我们经常使用Firefox的firebug或者Chrome的开发人员工具进行Web调试页面,Jav ...

  10. Log4J简单使用

    一.一般会将commons-logging和Log4j一起使用   原因:1.commons-logging功能较弱 2.log4j功能强大. 所需jar:       log4j-1.2.16.ja ...