题目:

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

  • addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.
  • queryRange(int left, int right) Returns true if and only if every real number in the interval [left, right) is currently being tracked.
  • removeRange(int left, int right) Stops tracking every real number currently being tracked in the interval [left, right).

Example 1:

addRange(10, 20): null
removeRange(14, 16): null
queryRange(10, 14): true (Every number in [10, 14) is being tracked)
queryRange(13, 15): false (Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
queryRange(16, 17): true (The number 16 in [16, 17) is still being tracked, despite the remove operation)

Note:

  • A half open interval [left, right) denotes all real numbers left <= x < right.
  • 0 < left < right < 10^9 in all calls to addRange, queryRange, removeRange.
  • The total number of calls to addRange in a single test case is at most 1000.
  • The total number of calls to queryRange in a single test case is at most 5000.
  • The total number of calls to removeRange in a single test case is at most 1000.

分析:

维护一个区间集合,支持增加和删除区间的操作,当queryRange也就是查询某个区间是否存在时,如果是已有区间包含查询的区间就返回true。

使用一个List用来存储所有的区间,在add方法中保持List中的区间有序。每一次add新区间时,都遍历原来的List,找到待添加区间应插入的位置。

如果原来的区间和插入区间有重叠,就将它们合并。

查询区间可以通过二分法来查找。

程序:

class RangeModule {

    public RangeModule() {
range = new LinkedList<>();
} public void addRange(int left, int right) {
List<int[]> tempList = new LinkedList<>();
boolean insert = false;
for(int[] r:range){
if(right < r[0] && !insert){
tempList.add(new int[]{left, right});
insert = true;
}
if(r[0] > right ||r[1] < left)
tempList.add(r);
else{
left = Math.min(left, r[0]);
right = Math.max(right, r[1]);
}
}
if(!insert)
tempList.add(new int[]{left, right});
range = tempList;
} public boolean queryRange(int left, int right) {
int l = 0;
int r = range.size()-1;
while(l <= r){
int mid = l + (r - l) / 2;
int[] p = range.get(mid);
if(right < p[0]){
r = mid - 1;
}else if(p[1] < left){
l = mid + 1;
}else{
return p[0] <= left && p[1] >= right;
}
}
return false;
} public void removeRange(int left, int right) {
List<int[]> tempList = new LinkedList<>();
for(int[] r:range){
if(r[0] > right || r[1] < left)
tempList.add(r);
else{
if(r[0] < left)
tempList.add(new int[]{r[0], left});
if(r[1] > right)
tempList.add(new int[]{right, r[1]});
}
}
range = tempList;
}
private List<int[]> range;
} /**
* Your RangeModule object will be instantiated and called as such:
* RangeModule obj = new RangeModule();
* obj.addRange(left,right);
* boolean param_2 = obj.queryRange(left,right);
* obj.removeRange(left,right);
*/

LeetCode 715. Range Module Range 模块 (Java)的更多相关文章

  1. [LeetCode] Range Module 范围模块

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  2. [Swift]LeetCode715. Range 模块 | Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  3. 715. Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  4. Range Module

    2019-09-21 18:54:16 715. Range Module 问题描述: 问题求解: 用线段树解决了. class RangeModule { Node root; class Node ...

  5. 【设计模式】module(模块)模式

    写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...

  6. IDEA报错Error:Module 'shop-common' production: java.lang.IndexOutOfBoundsException

    问题描述: 本来项目是正常的,编译.运行.启动都是OK的,但是在一次电脑重启后,出现了以上这个问题:Error:Module 'shop-common' production: java.lang.I ...

  7. 安卓与Unity交互之-Android Studio创建Module库模块教程

    安卓开发工具创建Module库 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  8. AngularJS系统学习之Module(模块)

    本文源自:http://blog.csdn.net/woxueliuyun/article/details/50962645 学习之后略有所得, 来此分享.建议看原文. 模块是提供一些特殊服务的功能块 ...

  9. Java实现 LeetCode 715 Range 模块(选范围)

    715. Range 模块 Range 模块是跟踪数字范围的模块.你的任务是以一种有效的方式设计和实现以下接口. addRange(int left, int right) 添加半开区间 [left, ...

  10. LeetCode算法题-Range Addition II(Java实现)

    这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...

随机推荐

  1. IT人的年夜饭,也太香了吧

    ​简介: 平时的IT人,奋战在修复bug前线,起早与贪黑齐飞,调休共假期待定.到了新春佳节,对于IT人来说,没有什么是比一顿年夜饭更让人熨贴肺腑的了.为了让废寝忘食编程序.闻机起早保运维的IT人过一个 ...

  2. 实时数仓入门训练营:实时计算 Flink 版 SQL 实践

    ​简介: <实时数仓入门训练营>由阿里云研究员王峰.阿里云资深技术专家金晓军.阿里云高级产品专家刘一鸣等实时计算 Flink 版和 Hologres 的多名技术/产品一线专家齐上阵,合力搭 ...

  3. 5分钟搞定AlertManager接入短信、语音等10+种通知渠道

    ​简介: Alert Manager是开源监控系统Prometheus中用于处理告警信息的服务,通过将日志服务开放告警配置为Alert Manager中的一个Receiver,可以将Alert Man ...

  4. ABAP RSA 加密

    最近出现一些SAP ABAP RSA加密的需求,这里搬运一篇文章,用于学习参考. 本文链接:https://www.cnblogs.com/hhelibeb/p/14952732.html 原文标题: ...

  5. 2024-05-01:用go语言,给定两个长度为偶数n的整数数组nums1和nums2, 分别移除它们各自的一半元素, 将剩下的元素合并成集合s。 找出集合s中可能包含的最多元素数量。 输入:nums

    2024-05-01:用go语言,给定两个长度为偶数n的整数数组nums1和nums2, 分别移除它们各自的一半元素, 将剩下的元素合并成集合s. 找出集合s中可能包含的最多元素数量. 输入:nums ...

  6. 七、Doris Colocation Join

    Colocation Join 是在 Doris 0.9 版本中引入的新功能.旨在为某些 Join 查询提供本地性优化,来减少数据在节点间的传输耗时,加速查询. 1.基本理论 Join 的常见连接类型 ...

  7. 21°C的冬天

    2023-12-08 16:15:36 星期五 标题没有在胡说,今天穿着初秋的衣服还嫌热,尤其是蒋震图书馆的空调更是燥热. 明天就去考教资面试了,但是一点也没有学习的兴趣,今天下午四点就写完了这周所有 ...

  8. 第五章-WAF 绕过

    WAF 绕过 1.WAF分类 1.1.软件 WAF 一般被安装到 Web 服务器中直接对其进行防护,能够接触到服务器上的文件,直接检测服务器上是否有不安全的文件和操作等. 常见的软件:安全狗.云盾.云 ...

  9. NumPy 数组创建方法与索引访问详解

    NumPy 创建数组 NumPy 中的核心数据结构是 ndarray,它代表多维数组.NumPy 提供了多种方法来创建 ndarray 对象,包括: 使用 array() 函数 array() 函数是 ...

  10. cesium教程3-加载3dtile模型,并调整位置

    直接上示例代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...