当前位置:网站首页>Leetcode - 359 log rate limiter (Design)
Leetcode - 359 log rate limiter (Design)
2022-07-25 15:40:00 【Cute at the age of three @d】


Hashtable

class Logger {
//printMap It records the time when the string was last printed
private Map<String,Integer> printMap;
public Logger() {
printMap = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if(printMap.get(message) == null)// character string message It hasn't been printed yet
{
// You can print
printMap.put(message,timestamp);
return true;
}
else{
// Last printed time
int last = printMap.get(message);
// You can print
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); */
queue + Hash set

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;
}
}
}
边栏推荐
- ZOJ - 4114 flipping game DP, reasonable state representation
- See a lot of blinking pictures on apps, especially the member page
- <栈模拟递归>
- Cf685b find the center of gravity of each subtree of a rooted tree
- Pytorch学习笔记--常用函数总结2
- matlab 如何保存所有运行后的数据
- In depth: micro and macro tasks
- 2021上海市赛-H-二分答案
- JVM—类加载器和双亲委派模型
- Graph theory and concept
猜你喜欢

《图书馆管理系统——“借书还书”模块》项目研发阶段性总结

GAMES101复习:三维变换

Take you to create your first C program (recommended Collection)

BPSK调制系统MATLAB仿真实现(1)

解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题

Ml speech depth neural network model

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

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28

Games101 review: Transformation

P4552 differential
随机推荐
BPSK调制系统MATLAB仿真实现(1)
Pat class a topic directory
Games101 review: linear algebra
数据系统分区设计 - 请求路由
JVM—类加载器和双亲委派模型
C # fine sorting knowledge points 10 generic (recommended Collection)
2021hncpc-e-difference, thinking
2019浙江省赛C-错排问题,贪心
理解“平均负载”
Idea - click the file code to automatically synchronize with the directory
CF685B-求有根树每颗子树的重心
Flex layout
Endnote 添加中文GBT7714样式 word中如何引用文献
《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
IOS interview questions
Flex 布局
LeetCode - 232 用栈实现队列 (设计 双栈实现队列)
PAT甲级1153 Decode Registration Card of PAT (25 分)
对this对象的理解
wait()和sleep()的区别理解