当前位置:网站首页>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);
*/
边栏推荐
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 请求参数的发送和接收
- Development experience and experience
- Complex character + number pyramid
- <, < <,>, > > Introduction in shell
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- Baidu editor ueeditor changes style
- Concurrent programming (VI) ABA problems and solutions under CAS
- JS non Boolean operation - learning notes
- Deep parsing JVM memory model
猜你喜欢
Es8 async and await learning notes
Dom4j遍历和更新XML
【Rust笔记】02-所有权
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
樹形DP AcWing 285. 沒有上司的舞會
Phpstudy 80 port occupied W10 system
Analysis of Alibaba canal principle
请求参数的发送和接收
Unity editor expansion - controls, layouts
Drawing maze EasyX library with recursive backtracking method
随机推荐
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
Find the combination number acwing 886 Find the combination number II
状态压缩DP AcWing 91. 最短Hamilton路径
求组合数 AcWing 886. 求组合数 II
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Gif remove blank frame frame number adjustment
LeetCode 871. 最低加油次数
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
【Rust 笔记】08-枚举与模式
DOM 渲染系统(render mount patch)响应式系统
【Rust 笔记】12-闭包
Really explain the five data structures of redis
[concurrent programming] concurrent tool class of thread
数位统计DP AcWing 338. 计数问题
Common DOS commands
[rust note] 10 operator overloading
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
单调栈-84. 柱状图中最大的矩形
On the difference and connection between find and select in TP5 framework
Character pyramid