当前位置:网站首页>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); */
边栏推荐
- CV learning notes - deep learning
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- openCV+dlib实现给蒙娜丽莎换脸
- Retinaface: single stage dense face localization in the wild
- LeetCode - 706 设计哈希映射(设计) *
- 20220531数学:快乐数
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
猜你喜欢

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

Flutter 退出当前操作二次确认怎么做才更优雅?

【C 题集】of Ⅵ

2. Elment UI date selector formatting problem

Swing transformer details-2

Discrete-event system

Swing transformer details-1
![[C question set] of Ⅵ](/img/49/eb31cd26f7efbc4d57f17dc1321092.jpg)
[C question set] of Ⅵ

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

Deep Reinforcement learning with PyTorch
随机推荐
LeetCode - 705 设计哈希集合(设计)
Dynamic layout management
Swing transformer details-2
Yocto Technology Sharing Phase 4: Custom add package support
Swing transformer details-1
Label Semantic Aware Pre-training for Few-shot Text Classification
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
01 business structure of imitation station B project
LeetCode - 5 最长回文子串
CV learning notes convolutional neural network
CV learning notes - feature extraction
Leetcode-404: sum of left leaves
QT detection card reader analog keyboard input
4.1 Temporal Differential of one step
Leetcode 300 最长上升子序列
Flutter 退出当前操作二次确认怎么做才更优雅?
Adaptiveavgpool1d internal implementation
The data read by pandas is saved to the MySQL database
Label Semantic Aware Pre-training for Few-shot Text Classification
Crash工具基本使用及实战分享