mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>写一个 <code>RecentCounter</code> 类来计算特定时间范围内最近的请求。</p>
 | 
						||
 | 
						||
<p>请你实现 <code>RecentCounter</code> 类:</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>RecentCounter()</code> 初始化计数器,请求数为 0 。</li>
 | 
						||
	<li><code>int ping(int t)</code> 在时间 <code>t</code> 添加一个新请求,其中 <code>t</code> 表示以毫秒为单位的某个时间,并返回过去 <code>3000</code> 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 <code>[t-3000, t]</code> 内发生的请求数。</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p><strong>保证</strong> 每次对 <code>ping</code> 的调用都使用比之前更大的 <code>t</code> 值。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>
 | 
						||
["RecentCounter", "ping", "ping", "ping", "ping"]
 | 
						||
[[], [1], [100], [3001], [3002]]
 | 
						||
<strong>输出:</strong>
 | 
						||
[null, 1, 2, 3, 3]
 | 
						||
 | 
						||
<strong>解释:</strong>
 | 
						||
RecentCounter recentCounter = new RecentCounter();
 | 
						||
recentCounter.ping(1);     // requests = [<strong>1</strong>],范围是 [-2999,1],返回 1
 | 
						||
recentCounter.ping(100);   // requests = [<strong>1</strong>, <strong>100</strong>],范围是 [-2900,100],返回 2
 | 
						||
recentCounter.ping(3001);  // requests = [<strong>1</strong>, <strong>100</strong>, <strong>3001</strong>],范围是 [1,3001],返回 3
 | 
						||
recentCounter.ping(3002);  // requests = [1, <strong>100</strong>, <strong>3001</strong>, <strong>3002</strong>],范围是 [2,3002],返回 3
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= t <= 10<sup>9</sup></code></li>
 | 
						||
	<li>保证每次对 <code>ping</code> 调用所使用的 <code>t</code> 值都 <strong>严格递增</strong></li>
 | 
						||
	<li>至多调用 <code>ping</code> 方法 <code>10<sup>4</sup></code> 次</li>
 | 
						||
</ul>
 |