【LeetCode】详解最近的请求次数933. Number of Recent Calls Write a class RecentCounter to count recent requests.


前言

今天带来的是一道简单的LeetCode题,废话少说,直接看题。

不学习,你就废了。


正文

原题

链接:最近的请求次数

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 pings 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 to ping.
Each test case will call ping with strictly increasing values of t.
Each call to ping will have 1 <= t <= 10^9.

题目大意
这道题的很多人都看不懂,然后就切换到中文版,结果还是看不懂,中文版的翻译如下:
在这里插入图片描述
当然,还是有很多人看不懂这道题,如同这些网友的评论:
在这里插入图片描述
不过我觉得还好,可能是我之前面试时写过LRU,多看几遍就可以明白题目的意思了。

题目就是让我们获取最近的请求次数,而最近的请求次数其实就是最近3000ms内的请求次数。联系实际应用,既然要获取最近3000ms内的请求,那我们肯定需要用到时间,比如记录当前请求的时间戳,题目没有类似时间戳的东西,但是给我们提供了一个参数t,ping(int t)这个t我们可以理解为时间戳。

再举个容易懂的例子🌰

  • 我们在2020年01月08日09:00:00请求了第一次,每次请求时就会返回当前的请求次数,即为1。伪代码的表示为int count = ping("2020年01月08日09:00:00"),此时的count为1
  • 2020年01月08日09:00:01请求了第二次,伪代码的表示为int count = ping("2020年01月08日09:00:01"),此时的count为2
  • 2020年01月08日09:00:02请求了第三次,伪代码的表示为int count = ping("2020年01月08日09:00:02"),此时的count为3
  • 那如果我们是在2020年01月08日09:00:10请求了第四次,大家觉得count为多少?(请读者思考一下)
  • 如果你
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值