当前位置:网站首页>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); */
边栏推荐
- 4G module IMEI of charging pile design
- LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
- 使用sed替换文件夹下文件
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
- Education is a pass and ticket. With it, you can step into a higher-level environment
- Installation and removal of MySQL under Windows
- The underlying principle of vector
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Opencv feature extraction - hog
- pycharm 无法引入自定义包
猜你喜欢

Adaptiveavgpool1d internal implementation

Octave instructions

Windows下MySQL的安装和删除

Opencv Harris corner detection

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

Not many people can finally bring their interests to college graduation

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)

LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

Pymssql controls SQL for Chinese queries
随机推荐
STM32 interrupt switch
The data read by pandas is saved to the MySQL database
Leetcode bit operation
Swing transformer details-1
LeetCode - 919. 完全二叉树插入器 (数组)
Design of charging pile mqtt transplantation based on 4G EC20 module
03 fastjason solves circular references
03 FastJson 解决循环引用
Window maximum and minimum settings
Adaptiveavgpool1d internal implementation
I think all friends should know that the basic law of learning is: from easy to difficult
Cases of OpenCV image enhancement
It is difficult to quantify the extent to which a single-chip computer can find a job
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
ADS simulation design of class AB RF power amplifier
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
LeetCode - 900. RLE 迭代器
Basic knowledge of MySQL database (an introduction to systematization)
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
Development of intelligent charging pile (I): overview of the overall design of the system