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

Understanding the difference between wait() and sleep()

Ml speech depth neural network model

Brain racking CPU context switching

小波变换--dwt2 与wavedec2

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

wait()和sleep()的区别理解

JVM knowledge brain map sharing

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

Gary Marcus: 学习语言比你想象的更难

How to solve the login problem after the 30 day experience period of visual stuido2019
随机推荐
2021上海市赛-B-排序后dp
In depth: micro and macro tasks
Matlab randInt, matlab randInt function usage "recommended collection"
Pytorch学习笔记--Pytorch常用函数总结1
Find out what happened in the process of new
Cf685b find the center of gravity of each subtree of a rooted tree
Pytorch学习笔记--SEResNet50搭建
PageHelper does not take effect, and SQL does not automatically add limit
Qtime定义(手工废物利用简单好看)
SVD奇异值分解推导及应用与信号恢复
Take you to create your first C program (recommended Collection)
带你详细认识JS基础语法(建议收藏)
Games101 review: linear algebra
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
LeetCode - 641 设计循环双端队列(设计)*
谷歌博客:采用多重游戏决策Transformer训练通用智能体
p4552-差分
Pat grade a 1152 Google recruitment (20 points)
CF685B-求有根树每颗子树的重心
小波变换--dwt2 与wavedec2