当前位置:网站首页>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); */
边栏推荐
- What can I do to exit the current operation and confirm it twice?
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
- Development of intelligent charging pile (I): overview of the overall design of the system
- Dynamic layout management
- LeetCode - 705 设计哈希集合(设计)
- 4G module designed by charging pile obtains signal strength and quality
- LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
- On the problem of reference assignment to reference
- 20220603 Mathematics: pow (x, n)
猜你喜欢

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

Opencv histogram equalization

使用密钥对的形式连接阿里云服务器

CV learning notes - scale invariant feature transformation (SIFT)

Basic use and actual combat sharing of crash tool

CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)

CV learning notes alexnet

Pycharm cannot import custom package

Vgg16 migration learning source code
随机推荐
03 fastjason solves circular references
20220602数学:Excel表列序号
LeetCode - 919. 完全二叉树插入器 (数组)
4G module initialization of charge point design
Leetcode - 933 number of recent requests
2.2 DP: Value Iteration & Gambler‘s Problem
The data read by pandas is saved to the MySQL database
Leetcode-404: sum of left leaves
20220604数学:x的平方根
使用sed替换文件夹下文件
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
Opencv+dlib to change the face of Mona Lisa
4G module designed by charging pile obtains signal strength and quality
One click generate traffic password (exaggerated advertisement title)
LeetCode - 919. Full binary tree inserter (array)
Leetcode-100: same tree
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
Replace the files under the folder with sed
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
20220601 Mathematics: zero after factorial