715. Range 模块

Range 模块是跟踪数字范围的模块。你的任务是以一种有效的方式设计和实现以下接口。

addRange(int left, int right) 添加半开区间 [left, right),跟踪该区间中的每个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间 [left, right) 中尚未跟踪的任何数字到该区间中。

queryRange(int left, int right) 只有在当前正在跟踪区间 [left, right) 中的每一个实数时,才返回 true。

removeRange(int left, int right) 停止跟踪区间 [left, right) 中当前正在跟踪的每个实数。

示例:

  1. addRange(10, 20): null
  2. removeRange(14, 16): null
  3. queryRange(10, 14): true (区间 [10, 14) 中的每个数都正在被跟踪)
  4. queryRange(13, 15): false (未跟踪区间 [13, 15) 中像 14, 14.03, 14.17 这样的数字)
  5. queryRange(16, 17): true (尽管执行了删除操作,区间 [16, 17) 中的数字 16 仍然会被跟踪)

提示:

  1. 半开区间 [left, right) 表示所有满足 left <= x < right 的实数。
  2. addRange, queryRange, removeRange 的所有调用中 0 < left < right < 10^9
  3. 在单个测试用例中,对 addRange 的调用总数不超过 1000 次。
  4. 在单个测试用例中,对 queryRange 的调用总数不超过 5000 次。
  5. 在单个测试用例中,对 removeRange 的调用总数不超过 1000 次。
  1. class RangeModule {
  2. TreeSet<Interval> ranges;
  3. public RangeModule() {
  4. ranges = new TreeSet();
  5. }
  6. public void addRange(int left, int right) {
  7. Iterator<Interval> itr = ranges.tailSet(new Interval(0, left - 1)).iterator();
  8. while (itr.hasNext()) {
  9. Interval iv = itr.next();
  10. if (right < iv.left) break;
  11. left = Math.min(left, iv.left);
  12. right = Math.max(right, iv.right);
  13. itr.remove();
  14. }
  15. ranges.add(new Interval(left, right));
  16. }
  17. public boolean queryRange(int left, int right) {
  18. Interval iv = ranges.higher(new Interval(0, left));
  19. return (iv != null && iv.left <= left && right <= iv.right);
  20. }
  21. public void removeRange(int left, int right) {
  22. Iterator<Interval> itr = ranges.tailSet(new Interval(0, left)).iterator();
  23. ArrayList<Interval> todo = new ArrayList();
  24. while (itr.hasNext()) {
  25. Interval iv = itr.next();
  26. if (right < iv.left) break;
  27. if (iv.left < left) todo.add(new Interval(iv.left, left));
  28. if (right < iv.right) todo.add(new Interval(right, iv.right));
  29. itr.remove();
  30. }
  31. for (Interval iv: todo) ranges.add(iv);
  32. }
  33. }
  34. class Interval implements Comparable<Interval>{
  35. int left;
  36. int right;
  37. public Interval(int left, int right){
  38. this.left = left;
  39. this.right = right;
  40. }
  41. public int compareTo(Interval that){
  42. if (this.right == that.right) return this.left - that.left;
  43. return this.right - that.right;
  44. }
  45. }
  46. /**
  47. * Your RangeModule object will be instantiated and called as such:
  48. * RangeModule obj = new RangeModule();
  49. * obj.addRange(left,right);
  50. * boolean param_2 = obj.queryRange(left,right);
  51. * obj.removeRange(left,right);
  52. */

Java实现 LeetCode 715 Range 模块(选范围)的更多相关文章

  1. [Swift]LeetCode715. Range 模块 | Range Module

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  2. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. Algorithms - Priority Queue - 优先队列

    Priority queue - 优先队列 相关概念 Priority queue优先队列是一种用来维护由一组元素构成的集合S的数据结构, 其中的每一种元素都有一个相关的值,称为关键字(key). 一 ...

  2. 如何在MATLAB下把模糊推理系统转化为查询表(转载)

    如何在MATLAB下把模糊推理系统转化为查询表(原创) http://foundy.blog.163.com/blog/static/2633834420090212202156/?mode=edit ...

  3. [poj1741 Tree]树上点分治

    题意:给一个N个节点的带权树,求长度小于等于K的路径条数 思路:选取一个点作为根root,假设f(root)是当前树的答案,那么答案来源于两部分: (1)路径不经过root,那么就是完全在子树内,这部 ...

  4. [ACdream 1215 Get Out!]判断点在封闭图形内, SPFA判负环

    大致题意:在二维平面上,给一些圆形岛屿的坐标和半径,以及圆形船的位置和半径,问能否划到无穷远的地方去 思路:考虑任意两点,如果a和b之间船不能通过,则连一条边,则问题转化为判断点是否在多边形中.先进行 ...

  5. My sql的知识点 不足点请指点谢谢

    My SQL:(关系数据库) 数据库能够能够干吗? 1. :存储大量的信息,方便检索和访问    2. :保持数据信息的一致,完整      3. : 共享和安全 4. : 通过组合分析,产生新的有用 ...

  6. vue学习-第三个DEMO(计算属性和监视) v-model基础用法

    <div id="demo"> 姓:<input type="text" placeholder="First Name" ...

  7. Angular第三方UI组件库------ionic

    一.Angular  UI组件库  ------------ionic 1. 官网:https://ionicframework.com 文档:https://ionicframework.com/d ...

  8. Django模板之模板变量过滤器

    在Django的模板语言中,通过使用 过滤器 来改变变量的显示:Django的模板语言中提供了大约六十个内置过滤器. 过滤器规则: ·         过滤器的语法: {{ value|filter_ ...

  9. Redis-主从

    主从复制过程 1.从服务器开始连接主服务器时,会向主服务器发送一个SYNC同步命令 2.主服务器接收到命令后,执行BGSAVE,异步的将写命令保存到一个缓冲区里 3.主服务器执行完BGSAVE之后,就 ...

  10. 索引 'GXHRCS.PK_A253' 或这类索引的分区处于不可用状态

    ORA-01502: 索引 'GXHRCS.PK_A253' 或这类索引的分区处于不可用状态 http://blog.sina.com.cn/s/blog_7ab8d2720101ozw6.html ...