当前位置:网站首页>LeetCode - 715. Range 模块(TreeSet) *****
LeetCode - 715. Range 模块(TreeSet) *****
2022-07-03 09:20:00 【三岁就很萌@D】
TreeSet
class RangeModule {
class Pair{
int left;
int right;
public Pair(int left,int right){
this.left = left;
this.right = right;
}
}
class PairComparator implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
if(p1.right == p2.right)
return p1.left - p2.left;
else
return p1.right - p2.right;
}
}
private TreeSet<Pair> set;
public RangeModule() {
set = new TreeSet<>(new PairComparator());
}
public void addRange(int left, int right) {
//tailSet(E fromElement):返回>=fromElement值的集合元素
Iterator<Pair> itr = set.tailSet(new Pair(0,left)).iterator();
while(itr.hasNext()){
Pair pair = itr.next();
if(right < pair.left)
break;
left = Math.min(left,pair.left);
right = Math.max(right,pair.right);
itr.remove();
}
set.add(new Pair(left,right));
}
public boolean queryRange(int left, int right) {
//public E higher(E e):返回严格大于给定键值的最小键值
Pair pair = set.higher(new Pair(0,left));
return (pair != null && pair.left <= left && right <= pair.right);
}
public void removeRange(int left, int right) {
Iterator<Pair> itr = set.tailSet(new Pair(0,left)).iterator();
ArrayList<Pair> todo = new ArrayList();
while(itr.hasNext()){
Pair pair = itr.next();
if(right < pair.left)
break;
if(left > pair.left) todo.add(new Pair(pair.left,left));
if(right < pair.right) todo.add(new Pair(right,pair.right));
itr.remove();
}
for(Pair pair: todo) set.add(pair);
}
}
/** * 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); */
边栏推荐
- ADS simulation design of class AB RF power amplifier
- Google browser plug-in recommendation
- Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- Screen display of charging pile design -- led driver ta6932
- Stm32 NVIC interrupt priority management
- Opencv interview guide
- Which language should I choose to program for single chip microcomputer
- In third tier cities and counties, it is difficult to get 10K after graduation
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- Leetcode 300 longest ascending subsequence
猜你喜欢
Working mode of 80C51 Serial Port
My notes on intelligent charging pile development (II): overview of system hardware circuit design
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
pycharm 无法引入自定义包
Which language should I choose to program for single chip microcomputer
Opencv histogram equalization
Notes on C language learning of migrant workers majoring in electronic information engineering
MySQL root user needs sudo login
Adaptiveavgpool1d internal implementation
JS基础-原型原型链和宏任务/微任务/事件机制
随机推荐
Tensorflow2.0 save model
使用sed替换文件夹下文件
2020-08-23
03 FastJson 解决循环引用
2020-08-23
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Modelcheckpoint auto save model
2.Elment Ui 日期选择器 格式化问题
getopt_ Typical use of long function
(2)接口中新增的方法
LeetCode - 900. RLE 迭代器
Education is a pass and ticket. With it, you can step into a higher-level environment
Leetcode 300 longest ascending subsequence
El table X-axis direction (horizontal) scroll bar slides to the right by default
Basic knowledge of MySQL database (an introduction to systematization)
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
Uniapp realizes global sharing of wechat applet and custom sharing button style
When the reference is assigned to auto
Yocto Technology Sharing Phase 4: Custom add package support
4G module at command communication package interface designed by charging pile