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.提供一个二 ...
随机推荐
- async与await暂停作用
1. async function Request () { await new Promise(res => { setTimeout(() => { console.log(1) re ...
- 【译】Visual Studio Enterprise 中的代码覆盖率特性
通过使用代码覆盖率功能,您可以发现您的测试需要改进的地方,并使您的软件更加健壮和可靠.在这篇文章中,我们将介绍我们在 Visual Studio Enterprise 2022 中引入的 Code C ...
- 新零售标杆 SKG 全面拥抱 Serverless,实现敏捷交付
简介: SKG CTO 王焱:以前需要 60 个人干的活,用 SAE 和大禹后 15 个人就可以! 作者:陈列昂.昕辰.龙琛.黛忻 项目背景 SKG 公司是一家专注于高端健康产品的研发.设计与制造 ...
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
简介: 这儿有一份"关于内存不足"排查实例,请查收. 文/系统运维 SIG 在<AK47 所向披靡,内存泄漏一网打尽>一文中,我们分享了slab 内存泄漏的排查方式和工 ...
- Serverless JOB | 传统任务新变革
简介: SAE Job 重点解决了用户的效率和成本问题,在兼具传统任务使用体验和功能的同时按需使用,按量计费,做到低门槛任务上云,节省闲置资源成本. Job 作为一种运完即停的负载类型,在企业级开发中 ...
- [FAQ] html 的 select 标签 option 获取选中值的两种方式及区别
Q: 对于一个 html 的 select 标签节点 class是module_select,获取选中值使用 $('.module_select').find('option:selected' ...
- [FAQ] "cannot refer to unexported name" in Golang ?
Golang 项目中如果使用了其它模块中找不到的函数.常量等,都会提示 "cannot refer to unexported name". 遇到这种情况,要么是拼写错误了,要么是 ...
- Vue3 和 Vue2 的异同及开发中具体区别
目录 总体来说 性能提升 树摇(Tree shaking) 碎片化节点(Fragment) 传送门 (Teleport) Suspense 更好的TypeScript支持 Composition AP ...
- SpringCloud + Seata1.5.0(使用docker安装配置Seata;数据存储mysql、配置中心与注册中心nacos)
1.seata介绍 Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.Seata 将为用户提供了 AT.TCC.SAGA 和 XA 事务模式,为用户打造一站式的分 ...
- JUC并发编程学习笔记(一)认知进程和线程
进程和线程 进程 一个程序,如QQ.exe,是程序的集合 一个进程往往可以包含多个线程,至少包含一个 java默认有两个线程,GC垃圾回收线程和Main线程 线程:一个进程中的各个功能 java无法真 ...