LeetCode 715. Range Module Range 模块 (Java)
题目:
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 numbersleft <= x < right. 0 < left < right < 10^9in all calls toaddRange, queryRange, removeRange.- The total number of calls to
addRangein a single test case is at most1000. - The total number of calls to
queryRangein a single test case is at most5000. - The total number of calls to
removeRangein a single test case is at most1000.
分析:
维护一个区间集合,支持增加和删除区间的操作,当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)的更多相关文章
- [LeetCode] Range Module 范围模块
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...
- [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 ...
- 715. Range Module
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...
- Range Module
2019-09-21 18:54:16 715. Range Module 问题描述: 问题求解: 用线段树解决了. class RangeModule { Node root; class Node ...
- 【设计模式】module(模块)模式
写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...
- IDEA报错Error:Module 'shop-common' production: java.lang.IndexOutOfBoundsException
问题描述: 本来项目是正常的,编译.运行.启动都是OK的,但是在一次电脑重启后,出现了以上这个问题:Error:Module 'shop-common' production: java.lang.I ...
- 安卓与Unity交互之-Android Studio创建Module库模块教程
安卓开发工具创建Module库 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- AngularJS系统学习之Module(模块)
本文源自:http://blog.csdn.net/woxueliuyun/article/details/50962645 学习之后略有所得, 来此分享.建议看原文. 模块是提供一些特殊服务的功能块 ...
- Java实现 LeetCode 715 Range 模块(选范围)
715. Range 模块 Range 模块是跟踪数字范围的模块.你的任务是以一种有效的方式设计和实现以下接口. addRange(int left, int right) 添加半开区间 [left, ...
- LeetCode算法题-Range Addition II(Java实现)
这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...
随机推荐
- 【Oracle】year must be between -4713 and +9999,and not be 0
[Oracle]year must be between -4713 and +9999,and not be 0 year must be between -4713 and +9999,and n ...
- 力扣1113(MySQL)-报告的记录(简单)
题目: 动作表:Actions 此表没有主键,所以可能会有重复的行. action 字段是 ENUM 类型的,包含:('view', 'like', 'reaction', 'comment', 'r ...
- Serverless 应用引擎 SAE 携手谱尼测试共同抗疫
简介:潮落江平未有风,扁舟共济与君同. 阿里云联合乘云至达与谱尼测试携手, 共同筑建抗疫堡垒,共抗疫情.共克时艰. 作者 | 计缘 背景 当前疫情形势依然严峻,各行各业众志成城,携手抗疫.新冠病毒核 ...
- 深度解析PolarDB数据库并行查询技术
简介: 随着数据规模的不断扩大,用户SQL的执行时间越来越长,这不仅对数据库的优化能力提出更高的要求,并且对数据库的执行模式也提出了新的挑战.本文将介绍基于代价进行并行优化.并行执行的云数据库的并行查 ...
- 使用率激增 250%,这份报告再次将 Serverless 推向幕前
简介: 本文是对 Datadog 最新的一份 Serverless 报告的解读,欢迎大家留言讨论. 本文是对 Datadog 最新的一份 Serverless 报告的解读,欢迎大家留言讨论. 每项新 ...
- WPF 已知问题 Separator 无法应用 ContextMenu 定义的默认样式
本文记录一个 WPF 已知问题,在 ContextMenu 的 Resources 里定义 Separator 的默认样式,在 ContextMenu 里面的 Separator 将应用不上,或者说不 ...
- dotnet 警惕 Task 的 ContinueWith 带上 OnlyOnFaulted 参数抛出取消异常
本文记录 dotnet 的一个令人迷惑的设计,在 Task 里,有一个叫 ContinueWith 的方法,此方法可以在 Task 完成时执行传入的委托.在 ContinueWith 方法里面,还有一 ...
- spannerlib优雅的go异常处理
蹩脚的go 异常处理 一般写go的人,如果他不是写算法,正常写业务代码的话,可能都会为优雅的异常处理而烦恼,因为脑子抽筋的go设计者们,总是感觉语法糖是一种很低级的东西.但是在我们大多数公司的业务逻辑 ...
- centos7虚拟机部署netcore3.1服务供局域网访问
如果买了亚马逊.腾讯.阿里等服务器,基本上几分钟就可以跑aspnetcore,外网访问分分钟.但是便宜点的服务器访问速度就没那么理想.这时候就需要考虑零成本的虚拟机部署了,当然这个基本都是局域网做测试 ...
- R5_ES读写流程
基本概念 refresh:es接收数据请求时先存入ES进程中的内存 Buffer ,默认每隔一秒(index.refresh_interval:1s)会从内存buffer中将数据写入 os cache ...