当前位置:网站首页>LeetCode - 715. Range module (TreeSet)*****
LeetCode - 715. Range module (TreeSet)*****
2022-07-03 10:12:00 【Cute at the age of three @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): return >=fromElement Set element of value
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): Returns the minimum key value that is strictly greater than the given key value
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); */
边栏推荐
- Qcombox style settings
- 2021-10-28
- What can I do to exit the current operation and confirm it twice?
- Basic use and actual combat sharing of crash tool
- Opencv note 21 frequency domain filtering
- 20220605数学:两数相除
- . DLL and Differences between lib files
- QT self drawing button with bubbles
- Vscode markdown export PDF error
- LeetCode - 5 最长回文子串
猜你喜欢
CV learning notes - feature extraction
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
The underlying principle of vector
My notes on intelligent charging pile development (II): overview of system hardware circuit design
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Opencv histogram equalization
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
CV learning notes - edge extraction
1. Finite Markov Decision Process
Swing transformer details-2
随机推荐
波士顿房价预测(TensorFlow2.9实践)
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
The underlying principle of vector
2021-10-27
LeetCode - 5 最长回文子串
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Cases of OpenCV image enhancement
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
CV learning notes ransca & image similarity comparison hash
CV learning notes - edge extraction
About windows and layout
4G module initialization of charge point design
20220601数学:阶乘后的零
RESNET code details
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Flutter 退出当前操作二次确认怎么做才更优雅?
Drive and control program of Dianchuan charging board for charging pile design
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
Problems encountered when MySQL saves CSV files