[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 ...
随机推荐
- C# AtomicBoolean
using System; using System.Threading; /// <summary> /// Provides lock-free atomic read/write u ...
- CSS源码之纯css3制作的哆啦a梦图片
本文章向大家介绍一个纯css3制作的哆啦a梦图像,主要巧妙的使用了css3的border-radius属性,需要的朋友介意参考一下本文章的源码. 效果图: 源码 <!doctype html&g ...
- ATM+购物商城
知识内容: 1.luffy买tesla 2.ATM+购物商城 一.luffy买tesla 需求: 1.目录结构说明 account luffy.json --> 存储用户账户信息 {" ...
- Java并发编程学习路线(转)
以前特地学过并发编程,但是没怎么学进去,不太喜欢.最近发现,作为一个资深工程师,却没有完整深入系统的学习过,而反是现在的BAT大并发是必须的,感觉甚是惭愧. 故找了一片学习文章,如下,准备集中一段时间 ...
- 得到当前对象在不同大小的页面中的绝对位置,及冒泡cancelBubble
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 关于jquery的cookie的顺序,应首先是jQuery的引用,然后是cookie引用,否则系统无反应
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript" ></scri ...
- 1.urlencoder和urldecoder的使用
今天传url的时候乱码了.先说情形,url中有searchText=中文的情形,后台new String(searchText.getBytes(ISO-8859-1),"gbk" ...
- class(类的使用说明)
class 的三大特性 封装:内部调用对于外部用户是透明的 继承: 在分类里的属性,方法被自动继承 多态:调用这个功能,可以使多个类同时执行 r1 = Role(r1, 'Alex', 'Police ...
- Spring MVC 自定义视图
实现View import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; ...
- Gradle 语法
参考文章: Gradle学习系列之二——创建Task的多种方法(http://www.cnblogs.com/CloudTeng/p/3417970.html) Gradle基本知识点与常用配置(ht ...