当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了

常量及数据类型你还记得多少?

Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using

基于UiAutomator2+PageObject模式开展APP自动化测试实战

APP automation testing practice based on UiAutomator2+PageObject mode

2022年裁员潮,失业程序员何去何从?

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
![[E-commerce operation] Do you really understand social media marketing (SMM)?](/img/5b/6682c613305deb3dc15401077d38a0.png)
[E-commerce operation] Do you really understand social media marketing (SMM)?

How many constants and data types do you remember?
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
随机推荐
不止跑路,拯救误操作rm -rf /*的小伙儿
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
皕杰报表在传参乱码
LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
leetcode 823. Binary Trees With Factors(因子二叉树)
越折腾越好用的 3 款开源 APP
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
不止跑路,拯救误操作rm -rf /*的小伙儿
力扣练习——60 二叉搜索子树的最大键值和
Introduction to Software Architecture
LeetCode_152_乘积最大子数组
力扣练习——56 寻找右区间
【小程序 | 启航篇】一文打通任督二脉
【机器学习】浅谈正规方程法&梯度下降
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
Analysis of the implementation principle of UUID from the perspective of source code
HDU 6040 Hints of sd0061 (技巧)
Article take you understand interrupt the key driver of polling mechanism
快速上手,征服三种不同分布式架构调用方案
[Go WebSocket] 多房间的聊天室(一)思考篇