Design a hit counter which counts the number of hits received in the past 5 minutes.

Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.

It is possible that several hits arrive roughly at the same time.

class HitCounter {

    Queue<Integer> queue;
/** Initialize your data structure here. */
public HitCounter() {
queue = new LinkedList<>();
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
queue.offer(timestamp);
} /** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
while(!queue.isEmpty() && timestamp - queue.peek() >= 300) {
queue.poll();
}
return queue.size();
}
} /**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/

[LC] 362. Design Hit Counter的更多相关文章

  1. LeetCode 362. Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...

  2. [LeetCode] 362. Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  3. 【LeetCode】362. Design Hit Counter 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  4. 362. Design Hit Counter

    这个傻逼题..我没弄明白 you may assume that calls are being made to the system in chronological order (ie, the ...

  5. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  6. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

  7. Design Hit Counter

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  8. AN ESAY HIT COUNTER

    <?php $counts = ("hitcounter.txt"); $hits = file($counts); $hits[0] ++; $fp = fopen($co ...

  9. LC 641. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

随机推荐

  1. awk 中 RS,ORS,FS,OFS 区别与联系

    一,RS与ORS 1,RS是记录分隔符,默认的分隔符是\n,具体用法看下 [root@krlcgcms01 mytest]# cat test1     //测试文件 111 222 333 444 ...

  2. 复杂分布式架构下的计算治理之路:计算中间件 Linkis

    前言 在当前的复杂分布式架构环境下,服务治理已经大行其道.但目光往下一层,从上层 APP.Service,到底层计算引擎这一层面,却还是各个引擎各自为政,Client-Server 模式紧耦合满天飞的 ...

  3. 吴裕雄--天生自然ShellX学习笔记:Shell 变量

    定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一 ...

  4. 安装完Ubuntu后没有设置过root密码,想要进入root账户怎么办?

    安装完Ubuntu后没有设置过root密码,想要进入root账户怎么办? Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd,然 ...

  5. this关键字使用注意事项

    1.当局部变量和成员变量重名时 ,java会启用就近原则,为了区分成员变量,最好再成员变量中加上this(this.成员变量),this的最主要的作用就是处理成员变量和局部变量重名的问题 例如,set ...

  6. java内部类(构造spring中的接收返回数据的实体类)

    说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能用到的场景不多,用得最多的是在有事件监听的情况下,并且即使用到也很少去总结内部类的用法.今天我们就来一探究竟. 原文链 ...

  7. 理论优美的深度信念网络--Hinton北大最新演讲

    什么是深度信念网络 深度信念网络是第一批成功应用深度架构训练的非卷积模型之一. 在引入深度信念网络之前,研究社区通常认为深度模型太难优化,还不如使用易于优化的浅层ML模型.2006年,Hinton等研 ...

  8. mysql安装(centos7)

    1.下载rpm wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 2.安装rpm yum ...

  9. BTree

    hash.平衡二叉树.BTree.B+tree的区别 https://blog.csdn.net/qq_40673786/article/details/90082444 联合索引在B+树上的结构介绍 ...

  10. drf框架概况-resful接口规范-请求模块-渲染模块-Postman-drf请求生命周期

    drf框架 全称:django-rest- framework 知识点: """ 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码-基于restful ...