当前位置:网站首页>LeetCode 362. Design Hit Counter(计数器)
LeetCode 362. Design Hit Counter(计数器)
2022-08-10 11:09:00 【水菜笔】
该题被锁,访问题解地址:https://blog.csdn.net/jmspan/article/details/51738164
hit(timestamp)传入一个时刻,表示在该时刻内敲击了;
gethit(timestamp) 传入一个时刻,表示在该时刻的5分钟内的敲击次数
使用一个map,保存敲击的时刻和次数。返回敲击次数的时候,遍历整个map;如果超时了,就删除。只保留5分钟之内的。
int count = 0;
Map<Integer, Integer> map = new HashMap<>();
public void hit(int timestamp) {
count++;
int result = map.getOrDefault(timestamp,0) + 1;
map.put(timestamp, result);
// getHits(timestamp);
}
public int getHits(int timestamp) {
Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
Map.Entry<Integer, Integer> entry;
while (iterator.hasNext()) {
entry = iterator.next();
if (timestamp-entry.getKey() >= 300) {
iterator.remove();
count-=entry.getValue();
}
}
return count;
}
边栏推荐
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 力扣练习——62 有效的数独
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 项目部署、
- Short video software development - how to break the platform homogenization
- codevs 2370 小机房的树 (LCA)
- 第二十二章 源代码文件 REST API 参考(四)
- Emulate stm32 directly with proteus - the programmer can be completely discarded
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- 接口定义与实现
猜你喜欢
随机推荐
怎么加入自媒体,了解这5种变现模式,让账号快速变现
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
程序员追求技术夯实基础学习路线建议
做自媒体月入几万?博主们都在用的几个自媒体工具
Open Office XML 格式里如何描述多段具有不同字体设置的段落
Clicking Exercise - 64 Longest Harmonic Subsequences
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
关于振弦采集模块及采集仪振弦频率值准确率的问题
Buckle Exercise - 61 Sort by frequency of characters
【勇敢饭饭,不怕刷题之链表】有序链表的合并
力扣练习——58 验证二叉搜索树
[E-commerce operation] Do you really understand social media marketing (SMM)?
POJ 1026 Cipher (置换群)
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
第2章-矩阵及其运算-矩阵运算(2)
std::move()
推荐6个自媒体领域,轻松易上手
Spss-多元回归案例实操