当前位置:网站首页>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);
*/
边栏推荐
- 【微信小程序:缓存操作】
- Greenplum6.x-版本变化记录-常用手册
- [wechat applet: cache operation]
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- Three usage scenarios of annotation @configurationproperties
- Required String parameter ‘XXX‘ is not present
- 基本数据类型和string类型互相转化
- Greenplum 6.x reinitialization
- IP地址的类别
- Speaking of a software entrepreneurship project, is there anyone willing to invest?
猜你喜欢
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Quick sorting (detailed illustration of single way, double way, three way)
关于基于kangle和EP面板使用CDN
Data type - floating point (C language)
Greenplum6.x重新初始化
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Teach you how to select PCB board by hand (II)
2-3 lookup tree
JS的操作
随机推荐
Componentspace2022, assertions, protocols, bindings, and configuration files
数据库存储---表分区
Category of IP address
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
Other 7 features of TCP [sliding window mechanism ▲]
Arm GIC (IV) GIC V3 register class analysis notes.
IP地址的类别
JS的操作
[Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
opencv之图像分割
QT charts use (rewrite qchartview to realize some custom functions)
注解@ConfigurationProperties的三种使用场景
Opencv converts 16 bit image data to 8 bits and 8 to 16
Gson转换实体类为json时报declares multiple JSON fields named
2-3 lookup tree
数字三角形模型 AcWing 275. 传纸条
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
[kuangbin] topic 15 digit DP