[leetcode]163. 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"]
题意:
给定一个区间和一堆数。统计一下在此区间有哪些数没在数组中出现,结果用区间表示。
思路:
这题跟[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缺失范围的更多相关文章
- [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 where the range of elements are in the inclusive range [lower, upper], ...
- ✡ 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 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 163. Missing Ranges
题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...
- 【LeetCode】Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- LC 163. Missing Ranges 【lock, hard】
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
随机推荐
- nginx的日志分析
1.到NGINX把日志DOWN下来2.用命令cat xxxx.log | egrep '10/Jul/2015:01:[4-5]|2015-07-10 02:0[0-57]'>xxxx2.log ...
- python + docker, 实现天气数据 从FTP获取以及持久化(五)-- 利用 Docker 容器化 Python 程序
背景 不知不觉中,我们已经完成了所有的编程工作.接下来,我们需要把 Python 程序 做 容器化 (Docker)部署. 思考 考虑到项目的实际情况,“持久化天气”的功能将会是一个独立的功能模块发布 ...
- 解决Sublime 3提示 Sublime Text Error while loading PyV8 binary
转自:http://blog.initm.com/sublime-text/ 今天打开sublime遇到一个提示 如上图Sublime Text Error while loading PyV8 b ...
- 自己写的jQuery拖动滑块
(function ($) { $.fn.bnSlide = function (options) { var defaults = { colorData: 0, //原始滑道的有效值 maxWid ...
- vs2013错误解决方法
1.cannot determine the location of the vs common tools folder 打开"VS2013开发人员命令提示后",上面提示&quo ...
- C# AtomicBoolean
using System; using System.Threading; /// <summary> /// Provides lock-free atomic read/write u ...
- C#中char空值的几种表示方式
C#中char空值的几种表示方式 在C#中char类型的表示方式通常是用单引号作为分隔符,而字符串是用双引号作为分隔符. 例如: 程序代码 程序代码 char a = 'a'; char b = 'b ...
- json 拖拽
1.梳理知识点 1.事件对象 e || event 2.事件对象的属性 鼠标事件对象 : 坐标属性 : clientX clientY pageX pageY offset ...
- opencv边缘检测的入门剖析(第七天)
---边缘检测概念理解--- 边缘检测的理解可以结合前面的内核,说到内核在图像中的应用还真是多,到现在为止学的对图像的操作都是核的操作,下面还有更神奇的! 想把边缘检测出来,从图像像素的角度去想,那就 ...
- [Flutter] Windows平台Flutter开发环境搭建(Andorid Studio)
前两天网友在群里说起了Flutter,就了解了一下,在手机上跑了它的demo,直接就被打动了. 虽然网上有很多教程,但真正开始的时候,还是会碰到很多坑.下面详细的讲解Flutter + Android ...