当前位置:网站首页>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;
}
}
}
边栏推荐
- Hdu3873 shortest path with dependency (topological sorting)
- MySQL transactions and mvcc
- Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件
- Cf365-e - Mishka and divisors, number theory +dp
- Games101 review: 3D transformation
- Delayed loading source code analysis:
- Word 样式模板复制到另一文档
- Pytorch框架练习(基于Kaggle Titanic竞赛)
- Qtime definition (manual waste utilization is simple and beautiful)
- 如何解决跨域问题
猜你喜欢

Get the ask code corresponding to the key pressed by the keyboard

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

matlab 优化工具 manopt 安装

How to solve the login problem after the 30 day experience period of visual stuido2019

ML - natural language processing - Key Technologies

Distributed principle - what is a distributed system

ML - natural language processing - Introduction to natural language processing

LeetCode - 225 用队列实现栈

ML - 语音 - 高级语音模型

matlab 如何保存所有运行后的数据
随机推荐
LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)
JS URLEncode function
Deadlock gossip
CF566A-贪心+字典树
2021hncpc-e-difference, thinking
ML - 自然语言处理 - 基础知识
Xcode added mobileprovision certificate file error: Xcode encoded an error
Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
Graph theory and concept
Seata中jdbc下只有一个lib嘛?
2021上海市赛-H-二分答案
CF888G-巧妙字典树+暴力分治(异或最小生成树)
p4552-差分
LeetCode - 622 设计循环队列 (设计)
二进制补码
我想问下变量配置功能是只能在SQL模式下使用吗
ML - 自然语言处理 - 关键技术
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
wait()和sleep()的区别理解
对this对象的理解