当前位置:网站首页>LeetCode 715. Range 模块
LeetCode 715. Range 模块
2022-07-03 08:49:00 【Sasakihaise_】
【有序集合】用TreeMap来进行有序集合的合并和拆分
class RangeModule {
// 区间拆分与合并 9:34 10:31
TreeMap<Integer, Integer> map = new TreeMap();
public RangeModule() {
}
public void addRange(int left, int right) {
Integer start = map.floorKey(right);
while(start != null){
int end = map.get(start);
if(end >= left){
left = Math.min(left, start);
right = Math.max(right, end);
map.remove(start);
}else{
break;
}
start = map.floorKey(right);
}
map.put(left, right);
}
public boolean queryRange(int left, int right) {
// for(var k: map.keySet()){
// System.out.print("[" + k + "," + map.get(k) + "],");
// }
// System.out.println();
// Integer start = map.floorKey(right);
// if(start == null) return false;
// if(start > left) return false;
// if(map.get(start) < right) return false;
// return true;
Integer start = map.floorKey(left);
if(start == null) return false;
if(map.get(start) >= right) return true;
return false;
}
public void removeRange(int left, int right) {
//. 3种情况
// start________end
// left_________right. [start, left], [end, right]X
// start________end
//. left______________right. [left, start]X, [end, right]X
//. start______________end
// left_______right [start, left], [right, end]
Integer start = map.floorKey(right);
while(start != null){
int end = map.get(start);
if(start == right) break;
if(end <= left) break;
else{
map.remove(start);
int s1 = Math.min(start, left);
int e1 = Math.max(start, left);
if(s1 == start && e1 == left && s1 != e1) map.put(s1, e1);
int s2 = Math.min(right, end);
int e2 = Math.max(right, end);
if(s2 == right && e2 == end && s2 != e2) {
map.put(s2, e2);
right --;
}
}
start = map.floorKey(right);
}
}
}
/**
* 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);
*/
边栏推荐
- JS ternary operator - learning notes (with cases)
- 20220630学习打卡
- MySQL index types B-tree and hash
- Monotonic stack -42 Connect rainwater
- Unity Editor Extension - drag and drop
- 【Rust 笔记】13-迭代器(上)
- Slice and index of array with data type
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- [rust notes] 08 enumeration and mode
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
猜你喜欢
Life cycle of Servlet
Character pyramid
JS non Boolean operation - learning notes
记忆化搜索 AcWing 901. 滑雪
Try to reprint an article about CSDN reprint
状态压缩DP AcWing 91. 最短Hamilton路径
Find the combination number acwing 885 Find the combination number I
【Rust笔记】02-所有权
注解简化配置与启动时加载
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
随机推荐
Really explain the five data structures of redis
Convert video to GIF
Redux - learning notes
Try to reprint an article about CSDN reprint
[redis] redis persistent RDB vs AOF (source code)
Deeply understand the underlying data structure of MySQL index
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Location of package cache downloaded by unity packagemanager
file_ put_ contents
Methods of using arrays as function parameters in shell
[rust notes] 06 package and module
[concurrent programming] Table hopping and blocking queue
C language file reading and writing
【Rust笔记】06-包和模块
Servlet的生命周期
【Rust 笔记】08-枚举与模式
JS non Boolean operation - learning notes
TP5 order multi condition sort
Too many open files solution
Unity editor expansion - controls, layouts