LeetCode Logger Rate Limiter
原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/
题目:
Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.
Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
It is possible that several messages arrive roughly at the same time.
Example:
Logger logger = new Logger(); // logging string "foo" at timestamp 1
logger.shouldPrintMessage(1, "foo"); returns true; // logging string "bar" at timestamp 2
logger.shouldPrintMessage(2,"bar"); returns true; // logging string "foo" at timestamp 3
logger.shouldPrintMessage(3,"foo"); returns false; // logging string "bar" at timestamp 8
logger.shouldPrintMessage(8,"bar"); returns false; // logging string "foo" at timestamp 10
logger.shouldPrintMessage(10,"foo"); returns false; // logging string "foo" at timestamp 11
logger.shouldPrintMessage(11,"foo"); returns true;
题解:
再次看到这题就像回到了当年的战场.
维护一个HashMap, key 是message, value 是该message的timestamp
没出现过的message加到HashMap中, return true.
出现过并没有超过10的message return false.
出现过并超过10的message, 跟新HashMap, return true.
Time Complexity: shouldPrintMessage O(1).
Space: HashMap的大小.
AC Java:
public class Logger {
/** Initialize your data structure here. */
HashMap<String, Integer> hm;
public Logger() {
hm = new HashMap<String, Integer>();
}
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
If this method returns false, the message will not be printed.
The timestamp is in seconds granularity. */
public boolean shouldPrintMessage(int timestamp, String message) {
if(!hm.containsKey(message)){
hm.put(message, timestamp);
return true;
}else if(timestamp - hm.get(message) < 10){
return false;
}else{
hm.put(message, timestamp);
return true;
}
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
LeetCode Logger Rate Limiter的更多相关文章
- [LeetCode] Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...
- LeetCode 359. Logger Rate Limiter (记录速率限制器)$
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] 359. Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- LeetCode 359 Logger Rate Limiter
Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...
- Logger Rate Limiter -- LeetCode
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- 【LeetCode】359. Logger Rate Limiter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- Logger Rate Limiter 十秒限时计数器
[抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...
- Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
随机推荐
- 51nod1073(约瑟夫环)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1073 题意: 中文题诶~ 思路: 直接模拟的话O(n*k)的 ...
- eclipse远程连接hive
创建项目,添加jar包,hive的s上,所以也需要hadoop的一些jar 这个图片是从网上找的,我直接使用的以前hadoop的项目 创建测试类,写测试代码 //获取jdbc链接 private ...
- linux学习笔记-前篇
大学毕业已经快三年了,从事嵌入式开发的工作也快三年了. 不过,老干些裸机开发,感觉很是枯燥,一咬牙一跺脚,决定从今天开始学习Linux操作系统,顺便记录下学习过程中所遇到的问题与心得. 自己从前完全没 ...
- 简单Excel表格上传下载,POI
一.废话 Excel表格是office软件中的一员,几乎是使用次数最多的办公软件.所以在java进行企业级应用开发的时候经常会用到对应的上传下载便利办公. 目前,比较常用的实现Java导入.导出Exc ...
- log4php的配置
网上关于log4php配置的文章很多,下面是我的配置,跟网上部分略有不同 (1)添加日志 1.下载log4php,到官网就可以下载到,下载后解压 我的版本是log4php_2.3.0 ...
- html5和css3学习笔记
HTML5针对移动端,移动端的浏览器主要是chrome,是webkit内核; app(applicatin):应用; native app:原生的app sadsadsadad 单标签可以省略结尾标记 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- ZeroMQ接口函数之 :zmq_strerror - 获取ZMQ错误描述字符串
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_strerror zmq_strerror(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_ ...
- java读取xml文件
public ArrayList getMessage(){ String xmlFileName = null; List list = new ArrayList(); MessageBean m ...
- 使用功能强大的插件FastReport.Net打印报表实例
我第一次使用FastReport插件做的功能是打印一个十分复杂的excel表格,有几百个字段都需要绑定数据,至少需要4个数据源,而且用到横向.竖向合并单元格. 我不是直接连接数据库,而是使用Regis ...