LeetCode 359. 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;
题目标签:Hash Table
题目让我们设计一个 message 是否可以发送的检测器,只有在这个message 第一次发送,或者 上一次发送已经过去了10秒的情况下,才可以发送。
利用HashMap 来保存 message, 和它的 timestamp 就可以了。
Java Solution:
Runtime beats 49.91%
完成日期:06/06/2017
关键词:HashMap
关键点:保存message 和 timestamp
class Logger
{
HashMap<String, Integer> 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)) // first time for this message
{
map.put(message, timestamp);
return true;
}
else // at least second time for this message
{
if(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);
*/
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 359. Logger Rate Limiter (记录速率限制器)$的更多相关文章
- [LeetCode] 359. Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] 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 ...
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...
- 【LeetCode】359. Logger Rate Limiter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LC] 359. Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- LeetCode Logger Rate Limiter
原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...
- Logger Rate Limiter -- LeetCode
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- Logger Rate Limiter 十秒限时计数器
[抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...
随机推荐
- Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...
- Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些。
Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些. 常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Si ...
- mysql 存储引擎学习
现在我们常用的MySQL存储引擎主要是两种:InnoDB and MyISAM. 1.MyISAM 执行效率高 不支持事务 不支持外键 每个MyISAM在磁盘上存储成3个文件,其中文件名和表名都相同, ...
- CAD使用SetXData写数据(com接口)
主要用到函数说明: MxDrawEntity::SetXData 设置实体的扩展数据,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pXData 扩展数据链表 c#代码实现如下: ...
- 第三节:执行一些EF的增删改查
针对两表操作 一丶增加 #region 05-增加操作 /// <summary> /// 05-增加操作 /// </summary> /// <param name= ...
- JPQL 的基本使用
一.概念 JPQL 语言,即 Java Persistence Query Language 的简称.JPQL 和 HQL 是非常类似的,支持以面向对象的方式来写 SQL 语句,当然也支持本地的 SQ ...
- CF 429B B.Working out (四角dp)
题意: 两个人一个从左上角一个从左下角分别开始走分别走向右下角和右上角,(矩阵每个格子有数)问到达终点后可以得到的最大数是多少,并且条件是他们两个相遇的时候那个点的数不能算 思路: 首先这道题如果暴力 ...
- linux命令 info
info命令是Linux下info格式的帮助指令. 就内容来说,info页面比man page编写得要更好.更容易理解,也更友好,但man page使用起来确实要更容易得多.一个man page只有一 ...
- buf.readUIntBE()
buf.readUIntBE(offset, byteLength[, noAssert]) buf.readUIntLE(offset, byteLength[, noAssert]) offset ...
- PYGAME学习笔记_01
01_使用PYGAME创建图形窗口 1.1_游戏的初始化和退出 pygame.init() 写入并初始化所有PYGAME模块,使用其他模块之前,必须先调用init方法 pygame.quit() 卸载 ...