前言
今天带来的是一道简单的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为多少?(请读者思考一下) - 如果你