当前位置:网站首页>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;
}
}
}
边栏推荐
- Pat class a topic directory
- 2021 Jiangsu race a Array line segment tree, maintain value range, Euler power reduction
- 2019陕西省省赛K-变种Dijstra
- Singleton mode 3-- singleton mode
- Pat grade a 1152 Google recruitment (20 points)
- matlab randint,Matlab的randint函数用法「建议收藏」
- Games101 review: 3D transformation
- ML - 自然语言处理 - 自然语言处理简介
- Box avoiding mouse
- Phased summary of the research and development of the "library management system -" borrowing and returning "module
猜你喜欢

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

为什么PrepareStatement性能更好更安全?

MySQL transactions and mvcc

JVM - classloader and parental delegation model

MySQL - user and permission control

Idea eye care settings

LeetCode - 677 键值映射(设计)*

ML - natural language processing - Key Technologies

获取键盘按下的键位对应ask码

ML - Speech - advanced speech model
随机推荐
C # fine sorting knowledge points 12 exception handling (recommended Collection)
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
ML - natural language processing - Key Technologies
Submarine cable detector tss350 (I)
LeetCode - 622 设计循环队列 (设计)
2019 Shaanxi provincial competition j-bit operation + greed
Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
Gary Marcus: 学习语言比你想象的更难
ML - 自然语言处理 - 关键技术
See a lot of blinking pictures on apps, especially the member page
MySQL—常用SQL语句整理总结
2019陕西省省赛J-位运算+贪心
The difference between Apple buy in and apple pay
C#精挑整理知识要点9 集合2(建议收藏)
2021hncpc-e-difference, thinking
Flink-1.13.6版本的 Flink sql以yarn session 模式运行,怎么禁用托管
C # fine sorting knowledge points 10 generic (recommended Collection)
2019浙江省赛C-错排问题,贪心
2016CCPC网络选拔赛C-换根dp好题
CF750F1-思维dp