当前位置:网站首页>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);
*/
边栏推荐
- Iptables' state module (FTP service exercise)
- POJ - 3784 running medium
- Greenplum 6.x build_ Environment configuration
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- QT charts use (rewrite qchartview to realize some custom functions)
- let const
- [MySQL] detailed explanation of trigger content of database advanced
- [kuangbin] topic 15 digit DP
- xray的简单使用
- Data type - integer (C language)
猜你喜欢
Iptables' state module (FTP service exercise)
Quick sorting (detailed illustration of single way, double way, three way)
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Componentspace2022, assertions, protocols, bindings, and configuration files
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
Introduction to data fragmentation
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
Greenplum6.x常用语句
Novice entry SCM must understand those things
为什么要选择云原生数据库
随机推荐
POJ - 3784 running medium
Opencv converts 16 bit image data to 8 bits and 8 to 16
How to integrate app linking services in harmonyos applications
Category of IP address
数字三角形模型 AcWing 275. 传纸条
In go language, function is a type
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
更改当前文件夹及文件夹下文件日期shell脚本
Greenplum6.x-版本变化记录-常用手册
[Nanjing University] - [software analysis] course learning notes (I) -introduction
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Tips for using jeditabletable
Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
使用AGC重签名服务前后渠道号信息异常分析
[wechat applet: cache operation]
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
Virtual address space
go写一个在一定时间内运行的程序
[hard core science popularization] working principle of dynamic loop monitoring system
[MySQL] detailed explanation of trigger content of database advanced