当前位置:网站首页>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);
*/
边栏推荐
- Implementation method of data platform landing
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- Greenplum6.x监控软件搭建
- 如何在图片的目标中添加目标的mask
- Input of mathematical formula of obsidan
- redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
- [Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
- Required String parameter ‘XXX‘ is not present
- 关于基于kangle和EP面板使用CDN
- let const
猜你喜欢

Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]

Rapid integration of authentication services - harmonyos platform

Greenplum6.x搭建_安装

SSM integration

路由信息协议——RIP

数据分片介绍

Componentspace2022, assertions, protocols, bindings, and configuration files

Greenplum6.x重新初始化

Iptables' state module (FTP service exercise)
随机推荐
uniapp 微信小程序监测网络
Image segmentation in opencv
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
NCS Chengdu Xindian interview experience
下载和安装orcale database11.2.0.4
Find the original code, inverse code and complement of signed numbers [C language]
Installation and configuration of PLSQL
Iptables' state module (FTP service exercise)
Greenplum 6.x common statements
2-3查找樹
Why choose cloud native database
Opencv converts 16 bit image data to 8 bits and 8 to 16
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
Category of IP address
How to integrate app linking services in harmonyos applications
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
Implementation method of data platform landing
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
Novice entry SCM must understand those things
[kuangbin]专题十五 数位DP