当前位置:网站首页>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); */
边栏推荐
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- 20220603数学:Pow(x,n)
- Discrete-event system
- 20220607其他:两整数之和
- Retinaface: single stage dense face localization in the wild
- Leetcode-513:找树的左下角值
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Problems encountered when MySQL saves CSV files
- Opencv note 21 frequency domain filtering
- Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
猜你喜欢
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Octave instructions
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
Opencv notes 17 template matching
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
Leetcode-106:根据中后序遍历序列构造二叉树
LeetCode - 933 最近的请求次数
随机推荐
LeetCode - 933 最近的请求次数
RESNET code details
Opencv histogram equalization
Connect Alibaba cloud servers in the form of key pairs
20220531 Mathematics: Happy numbers
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Design of charging pile mqtt transplantation based on 4G EC20 module
Dynamic layout management
[C question set] of Ⅵ
After clicking the Save button, you can only click it once
Swing transformer details-2
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Discrete-event system
01 business structure of imitation station B project
4G module designed by charging pile obtains signal strength and quality
openCV+dlib實現給蒙娜麗莎換臉
Octave instructions
Opencv+dlib to change the face of Mona Lisa
Leetcode-100:相同的树
Vgg16 migration learning source code