mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			52 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你区间的 <strong>空</strong> 集,请你设计并实现满足要求的数据结构:</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><strong>新增:</strong>添加一个区间到这个区间集合中。</li>
 | 
						||
	<li><strong>统计:</strong>计算出现在 <strong>至少一个</strong> 区间中的整数个数。</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p>实现 <code>CountIntervals</code> 类:</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>CountIntervals()</code> 使用区间的空集初始化对象</li>
 | 
						||
	<li><code>void add(int left, int right)</code> 添加区间 <code>[left, right]</code> 到区间集合之中。</li>
 | 
						||
	<li><code>int count()</code> 返回出现在 <strong>至少一个</strong> 区间中的整数个数。</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p><strong>注意:</strong>区间 <code>[left, right]</code> 表示满足 <code>left <= x <= right</code> 的所有整数 <code>x</code> 。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入</strong>
 | 
						||
["CountIntervals", "add", "add", "count", "add", "count"]
 | 
						||
[[], [2, 3], [7, 10], [], [5, 8], []]
 | 
						||
<strong>输出</strong>
 | 
						||
[null, null, null, 6, null, 8]
 | 
						||
 | 
						||
<strong>解释</strong>
 | 
						||
CountIntervals countIntervals = new CountIntervals(); // 用一个区间空集初始化对象
 | 
						||
countIntervals.add(2, 3);  // 将 [2, 3] 添加到区间集合中
 | 
						||
countIntervals.add(7, 10); // 将 [7, 10] 添加到区间集合中
 | 
						||
countIntervals.count();    // 返回 6
 | 
						||
                           // 整数 2 和 3 出现在区间 [2, 3] 中
 | 
						||
                           // 整数 7、8、9、10 出现在区间 [7, 10] 中
 | 
						||
countIntervals.add(5, 8);  // 将 [5, 8] 添加到区间集合中
 | 
						||
countIntervals.count();    // 返回 8
 | 
						||
                           // 整数 2 和 3 出现在区间 [2, 3] 中
 | 
						||
                           // 整数 5 和 6 出现在区间 [5, 8] 中
 | 
						||
                           // 整数 7 和 8 出现在区间 [5, 8] 和区间 [7, 10] 中
 | 
						||
                           // 整数 9 和 10 出现在区间 [7, 10] 中</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= left <= right <= 10<sup>9</sup></code></li>
 | 
						||
	<li>最多调用  <code>add</code> 和 <code>count</code> 方法 <strong>总计</strong> <code>10<sup>5</sup></code> 次</li>
 | 
						||
	<li>调用 <code>count</code> 方法至少一次</li>
 | 
						||
</ul>
 |