当前位置:网站首页>力扣解法汇总933-最近的请求次数
力扣解法汇总933-最近的请求次数
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
写一个 RecentCounter 类来计算特定时间范围内最近的请求。
请你实现 RecentCounter 类:
RecentCounter() 初始化计数器,请求数为 0 。
int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。
保证 每次对 ping 的调用都使用比之前更大的 t 值。
示例 1:
输入:
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
输出:
[null, 1, 2, 3, 3]
解释:
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1],范围是 [-2999,1],返回 1
recentCounter.ping(100); // requests = [1, 100],范围是 [-2900,100],返回 2
recentCounter.ping(3001); // requests = [1, 100, 3001],范围是 [1,3001],返回 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002],范围是 [2,3002],返回 3
提示:
1 <= t <= 109
保证每次对 ping 调用所使用的 t 值都 严格递增
至多调用 ping 方法 104 次
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-recent-calls
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 队列的结果来存储最为合适,因为题目提供的t是递增的,所以每次加入数字每次从队列前开始遍历,如果不符合范围这移除,否则则退出循环。 * 最终只要返回队列数量即可。
代码:
public class Solution933 {
class RecentCounter {
Queue<Integer> queue = new ArrayDeque<>();
public RecentCounter() {
}
public int ping(int t) {
int i = t - 3000;
Iterator<Integer> iterator = queue.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
if (next < i) {
iterator.remove();
continue;
}
break;
}
queue.offer(t);
return queue.size();
}
}
}边栏推荐
- Freshman year: learning summary
- 2022 blind box applet app has become a new drainage outlet for enterprises
- Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?
- 力扣解法汇总965-单值二叉树
- What are the advantages of adaptive search advertising?
- Operating mechanism of Google ads bidding
- 如何提高广告的广告评级,也就是质量得分?
- Basedexclassloader
- How to restore the redis cluster and retain the complete cluster data after changing the node IP
- Bracket generation (backtracking)
猜你喜欢

Redis实现消息队列的4种方案

RPA introduction

Almost all schools will ask for the second round exam! Come in and recite the answer!

How to automatically color cells in Excel

Knowledge points of mall development

Does the virtual host have independent IP

Operating mechanism of Google ads bidding

MySQL advanced knowledge points

Why do we use Google search ads?

Google Ads 竞价的运作机制
随机推荐
Redis实现消息队列的4种方案
阿里云oss文件上传系统
Linux(CentOS7)安裝MySQL-5.7版本
如何让杀毒软件停止屏蔽某个网页?以GDATA为例
Alicloud OSS file upload system
Leetcode 55 jump game
Graphical data analysis | business analysis and data mining
How to automatically color cells in Excel
入手Ticwatch2
Spiral matrix (skill)
Linux(CentOS6)安装MySQL5.5版本数据库
How to restore the redis cluster and retain the complete cluster data after changing the node IP
UE4\UE5触摸屏touch事件:单指、双指
力扣解法汇总1022-从根到叶的二进制数之和
国资入股,建业地产这回稳了吗?
Tiobe - programming language ranking in June 2022
Software engineering - system flow chart
php开发09 文章模块的删除和文章分类编写
Four schemes for redis to implement message queue
Installing MySQL version 5.5 database for Linux (centos6)