博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
359. Logger Rate Limiter - Easy
阅读量:6370 次
发布时间:2019-06-23

本文共 1867 字,大约阅读时间需要 6 分钟。

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 1logger.shouldPrintMessage(1, "foo"); returns true; // logging string "bar" at timestamp 2logger.shouldPrintMessage(2,"bar"); returns true;// logging string "foo" at timestamp 3logger.shouldPrintMessage(3,"foo"); returns false;// logging string "bar" at timestamp 8logger.shouldPrintMessage(8,"bar"); returns false;// logging string "foo" at timestamp 10logger.shouldPrintMessage(10,"foo"); returns false;// logging string "foo" at timestamp 11logger.shouldPrintMessage(11,"foo"); returns true;

 

time: O(1), space: O(n)

class Logger {    Map
map; /** Initialize your data structure here. */ public Logger() { map = new HashMap<>(); } /** 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(!map.containsKey(message) || map.containsKey(message) && timestamp - map.get(message) >= 10) { map.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); */

 

转载于:https://www.cnblogs.com/fatttcat/p/10166836.html

你可能感兴趣的文章
特斯拉CEO马斯克父亲专访:亿万富翁是如何养成的?
查看>>
C 和 C++字符串详解
查看>>
Centos 7 安装 Xen
查看>>
Netty学习1—传统单线程服务端
查看>>
微信Token验证代码的实现
查看>>
【D3.js 学习总结】3、D3选择器
查看>>
C# 对于时间的相关问题
查看>>
小程序实现原理解析
查看>>
JavaScript 闭包
查看>>
java 调用 python(使用jpython)
查看>>
真实项目运用-RecyclerView封装
查看>>
Java 8 开发顶级技巧
查看>>
9月20-21日,十位阿里技术大牛带你玩转大流量与高并发
查看>>
软件工程之用户界面设计
查看>>
Jenkins安装入门
查看>>
Eclipse中SVN的安装步骤(两种)和使用方法
查看>>
[LeetCode] Self Crossing
查看>>
深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)
查看>>
回调函数
查看>>
【UNITY3D 游戏开发之九】两个调试程序的小细节(创建暂停脚本及UNITY REMOTE 4)
查看>>