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.提供一个二 ...
随机推荐
- 力扣521(java&python)-最长特殊序列Ⅰ(简单)
题目: 给你两个字符串 a 和 b,请返回 这两个字符串中 最长的特殊序列 的长度.如果不存在,则返回 -1 . 「最长特殊序列」 定义如下:该序列为 某字符串独有的最长子序列(即不能是其他字符串的 ...
- Web3开发者技术选型:前端视角(next.js)
引言 在现代Web开发的世界中,Web3技术的兴起为前端开发者开辟了新的可能性.Web3技术主要指的是建立在区块链基础上的分布式网络,使用户能够通过智能合约和去中心化应用(DApps)直接交互,而无需 ...
- 链栈的实现 C语言/C++
堆栈的链式存储C/C++实现--链栈 与顺序栈相比,链栈的优点在于不存在栈满上溢的问题.链栈通常使用单链表实现,进栈.出栈操作就是在单链表表头的 插入.删除操作.用单链表实现链栈时,使用不带头结点的单 ...
- 基于 EMR OLAP 的开源实时数仓解决方案之 ClickHouse 事务实现
简介:阿里云 EMR OLAP 与 Flink 团队深度合作,支持了 Flink 到 ClickHouse 的 Exactly-Once写入来保证整个实时数仓数据的准确性.本文介绍了基于 EMR O ...
- 谈谈JVM内部锁升级过程
简介: 对象在内存中的内存布局是什么样的?如何描述synchronized和ReentrantLock的底层实现和重入的底层原理?为什么AQS底层是CAS+volatile?锁的四种状态和锁升级过程应 ...
- 当设计模式遇上 Hooks
简介: 数据结构与设计模式能够指导我们在开发复杂系统中寻得一条清晰的道路,既然都说 Hooks 难以维护,那就尝试让「神」来拯救这混乱的局面.对于「设计模式是否有助于我们写出更优雅的 Hooks 」 ...
- dotnet 将任意时区的 DateTimeOffset 转换为中国时区时间文本
本文告诉大家在拿到任意时区的 DateTimeOffset 对象,将 DateTimeOffset 转换为使用中国的 +8 时区表示的时间 在开始之前,需要说明的是,采用 DateTimeOffset ...
- vue点击旋转,再点击复原
效果: 1.html.通过绑定t值控制不同的class名, 原图是右边方向的箭头 <img class="right" v-if="item.t" src ...
- SpringBoot项目预加载数据——ApplicationRunner、CommandLineRunner、InitializingBean 、@PostConstruct区别
0.参考.业务需求 参考: https://www.cnblogs.com/java-chen-hao/p/11835120.html#_label1 https://zhuanlan.zhihu.c ...
- 一、Doris演进史
Apache Doris -- 为分析而生 Doris发展历程: Doris发展比较重要的关键节点与事件 #2008 - Doris1 :「筑巢引凤」的重要基石 早年,百度最主要的收入来源是广告.广告 ...