当前位置:网站首页>LeetCode 715. Range 模块
LeetCode 715. Range 模块
2022-07-07 06:14:00 【Sasakihaise_】

【有序区间】区间拆分与合并,需要注意add的时候如果遇到[1, 10), [10, 11]这种要把他们合并起来,因为有可能查询[1, 11]这样的区间,因此这里用floorKey来找。
class RangeModule {
// 区间拆分与合并 7:21
TreeMap<Integer, Integer> map = new TreeMap();
public RangeModule() {
}
public void show() {
for (var k: map.keySet()) {
System.out.print("[" + k + "," + map.get(k) + "]");
}
System.out.println("====");
}
public void addRange(int left, int right) {
Integer key = map.floorKey(right);
while (key != null) {
int value = map.get(key);
if (value < left) {
break;
}
map.remove(key);
left = Math.min(key, left);
right = Math.max(value, right);
key = map.floorKey(right);
}
map.put(left, right);
// show();
}
public boolean queryRange(int left, int right) {
Integer key = map.floorKey(left);
if (key == null) {
return false;
}
if (map.get(key) >= right) {
return true;
}
return false;
}
public void removeRange(int left, int right) {
Integer key = map.lowerKey(right);
while (key != null) {
int value = map.get(key);
if (value <= left) {
break;
}
map.remove(key);
// key left value right
// key left right value
// left key value right
// left key right value
if (value > right) {
map.put(right, value);
}
if (key < left) {
map.put(key, left);
}
key = map.lowerKey(right);
}
// show();
}
}
/**
* 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);
*/
边栏推荐
- Three series of BOM elements
- ncs成都新電面試經驗
- A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
- How to realize the high temperature alarm of the machine room in the moving ring monitoring system
- Data analysis methodology and previous experience summary 2 [notes dry goods]
- 求有符号数的原码、反码和补码【C语言】
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- Introduction to data fragmentation
- In go language, function is a type
- 关于基于kangle和EP面板使用CDN
猜你喜欢

Greenplum 6.x monitoring software setup

Data type - floating point (C language)

In go language, function is a type

数字三角形模型 AcWing 275. 传纸条
![Upload an e-office V9 arbitrary file [vulnerability recurrence practice]](/img/e7/278193cbc2a2f562270f99634225bc.jpg)
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]

2-3查找樹

【踩坑】nacos注册一直连接localhost:8848,no available server

Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
![[hard core science popularization] working principle of dynamic loop monitoring system](/img/d4/0c0281aec5a4f444528e8cfd401598.jpg)
[hard core science popularization] working principle of dynamic loop monitoring system
随机推荐
Exercise arrangement 2.10, 11
go写一个在一定时间内运行的程序
ncs成都新電面試經驗
uniapp 微信小程序监测网络
Other 7 features of TCP [sliding window mechanism ▲]
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
A bug using module project in idea
注解@ConfigurationProperties的三种使用场景
NCS Chengdu New Electric interview Experience
Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
Compilation and linking of programs
Data type - floating point (C language)
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
AVL balanced binary search tree
Basic data types and string types are converted to each other
【MySQL】数据库进阶之触发器内容详解
Gson转换实体类为json时报declares multiple JSON fields named