当前位置:网站首页>LeetCode - 359 日志速率限制器 (设计)
LeetCode - 359 日志速率限制器 (设计)
2022-07-25 15:32:00 【三岁就很萌@D】


哈希表

class Logger {
//printMap记录了字符串最后被打印的时间
private Map<String,Integer> printMap;
public Logger() {
printMap = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if(printMap.get(message) == null)//字符串 message还没有被打印过
{
//可以打印
printMap.put(message,timestamp);
return true;
}
else{
//上一次被打印的时间
int last = printMap.get(message);
//可以打印
if(last+10 <= timestamp){
printMap.put(message,timestamp);
return true;
}
else
return false;
}
}
}
/** * Your Logger object will be instantiated and called as such: * Logger obj = new Logger(); * boolean param_1 = obj.shouldPrintMessage(timestamp,message); */
队列+哈希集合

class Logger {
class Node{
int timestamp;
String message;
public Node(int timestamp,String message){
this.message = message;
this.timestamp = timestamp;
}
}
Deque<Node> deque;
Set<String> set ;
public Logger() {
deque = new LinkedList<>();
set = new HashSet<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
while(deque.size()!=0 && deque.peekFirst().timestamp + 10 <= timestamp)
set.remove(deque.pollFirst().message);
if(set.contains(message))
return false;
else{
deque.offerLast(new Node(timestamp,message));
set.add(message);
return true;
}
}
}
边栏推荐
猜你喜欢

matlab 如何保存所有运行后的数据

伤透脑筋的CPU 上下文切换

Idea - click the file code to automatically synchronize with the directory

LeetCode - 379 电话目录管理系统(设计)

MySQL - user and permission control

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Games101 review: 3D transformation

Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果

LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)

GAMES101复习:三维变换
随机推荐
数据系统分区设计 - 分区与二级索引
Gary Marcus: 学习语言比你想象的更难
mouseover和mouseenter的区别
PAT甲级1153 Decode Registration Card of PAT (25 分)
Get the ask code corresponding to the key pressed by the keyboard
Pytorch学习笔记--Pytorch常用函数总结1
<栈模拟递归>
JVM knowledge brain map sharing
对this对象的理解
Pytorch学习笔记--SEResNet50搭建
ML - 语音 - 深度神经网络模型
JS URLEncode function
2019陕西省省赛J-位运算+贪心
ICPC2021昆明M-暴力+主席树
Cf888g clever dictionary tree + violent divide and conquer (XOR minimum spanning tree)
wait()和sleep()的区别理解
2019陕西省省赛K-变种Dijstra
LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)
MySQL transactions and mvcc
Games101 review: linear algebra