Logger Rate Limiter
2016-07-12 05:59
344 查看
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:
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;
public class Logger { /** Initialize your data structure here. */ HashMap<String, Integer> hashmap; public Logger() { hashmap = 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) { Integer k = hashmap.get(message); if(k == null){ hashmap.put(message, timestamp); return true; } else { if(timestamp - k >= 10){ hashmap.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); */
相关文章推荐
- 家里也是不知不觉就电脑有不能开启了
- JAVA SE8 OCA/OCP和PL/SQL OCA/OCP认证书籍推荐
- 热血江湖-术语整理
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- LintCode_178 Graph Valid Tree
- myeclipse项目转web项目
- Closest Binary Search Tree Value
- 利用dll创建窗口的一些学习过程
- javascript中Date对象的应用——简易日历的实现
- eclipse启动tomcat无法访问的解决方法
- BZOJ3224 普通平衡树
- Serializable的序列化与反序列化
- iGrimace V8 ios8系统越狱的手机安装方式:
- Java项目转web项目: Project facet Dynamic Web Module version 3.0 is not supported.
- Java9特性预览——Jshell
- Html5实现手机九宫格密码解锁功能
- 在老男孩教育的第一次作业
- 采购系统(一)
- 纯HTML5 Canvas实现的饼图
- js的中for in