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

题意:

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

思路:

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

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

代码:

 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. javascript面向对象的程序设计之Object.getOwnPropertyDescriptor()

    Object.getOwnPropertyDescriptor()用于获取给定属性的描述信息,这个描述信息是一个对象. 如果是访问器属性,则这个对象的属性有configurable,enumerabl ...

  2. C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号)

    最近学习过程中,想到提取系统硬件信息做一些验证,故而对网上提到的利用.NET System.Management类获取硬件信息做了进一步的学习.验证.验证是分别在4台电脑,XP SP3系统中进行,特将 ...

  3. Spark 分布式SQL引擎

    SparkSQL作为分布式查询引擎:两种方式 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 Spa ...

  4. python之ConfigParser

    以前傻傻的不知道还有configParser这么方便的模块,都是一个个的解析转换…… 配置文件xxxxx # 注释1 ;  注释2 [section1] # 节点 k1 = v1    # 值 k2: ...

  5. 柒月风华BBS上线

    论坛地址:https://3003soft.top/LBBS/ 欢迎大家加入. 开放式轻论坛:记录好玩.有趣的事儿:一起努力,一起前进: 希望能建立一个分享各类解决方案的社区

  6. 练手THINKPHP5过程和bootstrap3.3.7

    1 在GIT上下载了最新版的源码,同时安装composer 用composer更新 git地址https://github.com/top-think/think 2 搭建本地开发环境,开启url重写 ...

  7. mysql开启查询日志功能

    1.开启查询日志  https://www.cnblogs.com/kerrycode/p/7130403.html MYsql 查询日志配置    mysql> show variables ...

  8. Windows系统日常运维

    WINDOWS系统日常运维 http://www.docin.com/p-677263438.html

  9. 15. Studio上字符串转整形、整形转字符串例子

    var v1=ABS_SQLVALUE("select 1 from dual");var v2=ABS_SQLVALUE("select 2 from dual&quo ...

  10. GET和POST的真正区别

    文章来源: http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这 ...