当前位置:网站首页>LeetCode 362. Design Hit Counter
LeetCode 362. Design Hit Counter
2022-08-10 11:57:00 【Mizuna pen】
The question is locked,Visit the solution address:https://blog.csdn.net/jmspan/article/details/51738164
hit(timestamp)Pass in a moment,Indicates that a tap was made at that moment;
gethit(timestamp) Pass in a moment,means at that moment5分钟内的敲击次数
使用一个map,Save the time and number of taps.When returning the number of hits,遍历整个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;
}
边栏推荐
- 因为找不到lombok而找不到符号log
- 力扣练习——58 验证二叉搜索树
- 自媒体爆款标题怎么写?手把手教你写热门标题
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- Module 9 - Designing an e-commerce seckill system
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- ENVI 5.3软件安装包和安装教程
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- LeetCode 92. 反转链表 II
- Licking Exercise - 58 Verifying Binary Search Trees
猜你喜欢
随机推荐
关于振弦采集模块及采集仪振弦频率值准确率的问题
被面试官问到消息队列的丢失、重复与积压问题该如何回答
Network Fundamentals (Section 1)
不止跑路,拯救误操作rm -rf /*的小伙儿
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
【mysql】explain介绍[通俗易懂]
力扣练习——61 根据字符出现频率排序
使用.NET简单实现一个Redis的高性能克隆版(六)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
OSSCore 开源解决方案介绍
LeetCode 19. 删除链表的倒数第 N 个结点
为什么Redis很快
【Redis】内存回收策略
Nocalhost - Making development more efficient in the cloud-native era
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
SQL优化最强总结 (建议收藏~)
10 个 Reduce 常用“奇技淫巧”
LeetCode_443_压缩字符串
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions