[LeetCode] 933. Number of Recent Calls 最近的调用次数
Write a class `RecentCounter` to count recent requests.
It has only one method: ping(int t)
, where t represents some time in milliseconds.
Return the number of ping
s that have been made from 3000 milliseconds ago until now.
Any ping with time in [t - 3000, t]
will count, including the current ping.
It is guaranteed that every call to ping
uses a strictly larger value of t
than before.
Example 1:
Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]
Note:
- Each test case will have at most
10000
calls toping
. - Each test case will call
ping
with strictly increasing values oft
. - Each call to ping will have
1 <= t <= 10^9
.
这道题让实现一个 RecentCounter 类,里面有一个 ping 函数,输入给定了一个时间t,让我们求在 [t-3000, t] 时间范围内有多少次 ping。题目中限定了每次的给的时间一定会比上一次的时间大,而且只关心这个大小为 3001 的时间窗口范围内的次数,则利用滑动窗口 Sliding Window 来做就是个很不错的选择。由于数字是不断加入的,可以使用一个 queue,每当要加入一个新的时间点t时,先从队列开头遍历,若前面的时间不在当前的时间窗口内,则移除队列。之后再将当前时间点t加入,并返回队列的长度即可,参见代码如下:
class RecentCounter {
public:
RecentCounter() {}
int ping(int t) {
while (!q.empty()) {
if (q.front() + 3000 >= t) break;
q.pop();
}
q.push(t);
return q.size();
}
private:
queue<int> q;
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/933
参考资料:
https://leetcode.com/problems/number-of-recent-calls/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 933. Number of Recent Calls 最近的调用次数的更多相关文章
- 【Leetcode_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- 【LeetCode】933. Number of Recent Calls 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...
- LeetCode - Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- 109th LeetCode Weekly Contest Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...
- LeetCode.933-最近通话次数(Number of Recent Calls)
这是悦乐书的第357次更新,第384篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933).写一个类RecentCounter来计算最近的请求. 它 ...
- [Swift]LeetCode933. 最近的请求次数 | Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
随机推荐
- JAVA 数据库操作工具类----sqllite
package com.asc.db; import android.content.ContentValues; import android.content.Context; import and ...
- css 盒子模型简介
盒子模型 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- window和document的区别理解,bom和dom的区别理解
Window对象: 是整个BOM的核心,所有对象和集合都以某种方式回接到window对象.Window对象表示整个浏览器窗口,但不必表示其中包含的内容. Document对象: 实际上是window对 ...
- 引用类型--Date类型
要创建一个日期对象,使用new操作符和Date构造函数即可. var now = new Date() 在调用Date构造函数而不传递参数的情况下,新创建的对象自动获得当前日期和时间.如果想根据特定的 ...
- 《Netlogo多主体建模入门》笔记3
3- 用“生命游戏”认识Patch 代码: patches-own[living] to setup clear-all ask patches [ < 0.3[ set pcolo ...
- (编辑状态下)Reset及OnValidate使用
using UnityEngine; using System.Collections; public class Demo : MonoBehaviour { #if UNITY_EDITOR vo ...
- P1076 Wifi密码
P1076 Wifi密码 转跳点:
- 2. react 简书 头部(header) 图标添加
1. 访问 iconfont 并注册 登陆 2. 进入 iconfont 头部 图标管理->我的项目 3. 点击右边的文件夹 + 号 图标 创建我的项目 输入项目名称即可 4.在 搜索框 搜索 ...
- other#docker
阿里云docker镜像加速地址:https://cr.console.aliyun.com/#/accelerator docker 安装: yum install -y yum-utils devi ...
- 034、Java中自增之++在前面的写法
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...