-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange 模块 [range-module].html
41 lines (31 loc) · 2.26 KB
/
Range 模块 [range-module].html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<p>Range模块是跟踪数字范围的模块。设计一个数据结构来跟踪表示为 <strong>半开区间</strong> 的范围并查询它们。</p>
<p><strong>半开区间</strong> <code>[left, right)</code> 表示所有 <code>left <= x < right</code> 的实数 <code>x</code> 。</p>
<p>实现 <code>RangeModule</code> 类:</p>
<ul>
<li><code>RangeModule()</code> 初始化数据结构的对象。</li>
<li><code>void addRange(int left, int right)</code> 添加 <strong>半开区间</strong> <code>[left, right)</code>,跟踪该区间中的每个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间 <code>[left, right)</code> 中尚未跟踪的任何数字到该区间中。</li>
<li><code>boolean queryRange(int left, int right)</code> 只有在当前正在跟踪区间 <code>[left, right)</code> 中的每一个实数时,才返回 <code>true</code> ,否则返回 <code>false</code> 。</li>
<li><code>void removeRange(int left, int right)</code> 停止跟踪 <strong>半开区间</strong> <code>[left, right)</code> 中当前正在跟踪的每个实数。</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入</strong>
["RangeModule", "addRange", "removeRange", "queryRange", "queryRange", "queryRange"]
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
<strong>输出</strong>
[null, null, null, true, false, true]
<strong>解释</strong>
RangeModule rangeModule = new RangeModule();
rangeModule.addRange(10, 20);
rangeModule.removeRange(14, 16);
rangeModule.queryRange(10, 14); 返回 true (区间 [10, 14) 中的每个数都正在被跟踪)
rangeModule.queryRange(13, 15); 返回 false(未跟踪区间 [13, 15) 中像 14, 14.03, 14.17 这样的数字)
rangeModule.queryRange(16, 17); 返回 true (尽管执行了删除操作,区间 [16, 17) 中的数字 16 仍然会被跟踪)
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= left < right <= 10<sup>9</sup></code></li>
<li>在单个测试用例中,对 <code>addRange</code> 、 <code>queryRange</code> 和 <code>removeRange</code> 的调用总数不超过 <code>10<sup>4</sup></code> 次</li>
</ul>