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"]

题意:

给定一个区间和一堆数。统计一下在此区间有哪些数没在数组中出现,结果用区间表示。

思路:

这题跟[leetcode]228. Summary Ranges区间统计思路一致

区别是因为规定了  [lowerupper] 要考虑更多的边界

代码:

 class Solution {
public List<String> findMissingRanges(int[] a, int lower, int upper) {
List<String> result = new ArrayList<>(); if(a.length == 0) {
group(result, lower, upper );
return result;
} if(a[0] != lower){
group(result, lower, a[0] - 1);
} for(int i = 1; i < a.length; i++ ){
if( a[i] != a[i-1] && a[i] != a[i-1] + 1){
group(result, a[i-1] + 1, a[i] - 1 );
}
} if(upper != a[a.length - 1] ){
group(result, a[a.length - 1] + 1 , upper);
}
return result;
} private void group(List<String> list, int start, int end){
if(start == end ){
list.add(start +"");
}else{
list.add(start + "->" + end );
}
}
}

[leetcode]163. Missing Ranges缺失范围的更多相关文章

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

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

  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 找出缺失范围 --------- java

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

  4. [LeetCode#163] Missing Ranges

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

  5. [LeetCode] Missing Ranges 缺失区间

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

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

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

  7. 163. Missing Ranges

    题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...

  8. 【LeetCode】Missing Ranges

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

  9. LC 163. Missing Ranges 【lock, hard】

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

随机推荐

  1. 设置UMG的ComboBox(String)字体大小

    转自:http://aigo.iteye.com/blog/2295448 UMG自带ComboBox组件没有提供直接的属性来修改其字体大小,只能自己做一个列表类型的widget然后再塞进ComboB ...

  2. How to use POST method in Tornado?

    http://stackoverflow.com/questions/10367981/how-to-use-post-method-in-tornado

  3. Maven 插件管理

    偶然与巧合 舞动了蝶翼 谁的心头风起 前赴而后继 万千人追寻 荒漠唯一菩提 似擦身相遇 或擦肩而去 命运犹如险棋 无数时间线 无数可能性 终于交织向你

  4. 6.27-JSTL、标签、分页

    一.JSTL 条件标签: <c:if> if if(){ }else if(){ }else{ } <c:choose> <c:when></c:when&g ...

  5. InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活

    InterlliJ IDEA 2017.3.x / 2017.3.4 License Server激活 1.Lincense Server激活 // 激活IDEA的License Server 地址 ...

  6. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  7. 《图像处理实例》 之 目标旋转矫正(基于区域提取、DFT变换)

    目标:1.把矩形旋转正.          2.把文字旋转校正.                                                                     ...

  8. docker使用笔记1

    rhel6安装 yum -y install docker-io ################################################ 进入容器命令 docker exec ...

  9. leetcode227

    class Solution { public: stack<int> OPD; stack<char> OPR; int calculate(int num1, int nu ...

  10. RabbitMQ Window环境安装

    转自:https://www.cnblogs.com/zzpblogs/p/8168763.html RabbitMQ环境的安装分别介绍在Window和Linux下两个环境的安装过程.   Windo ...