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 (记录速率限制器)$的更多相关文章

  1. [LeetCode] 359. Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  2. [LeetCode] Logger Rate Limiter 记录速率限制器

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  3. LeetCode 359 Logger Rate Limiter

    Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...

  4. 359. Logger Rate Limiter

    /* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...

  5. 【LeetCode】359. Logger Rate Limiter 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  6. [LC] 359. Logger Rate Limiter

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  7. LeetCode Logger Rate Limiter

    原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...

  8. Logger Rate Limiter -- LeetCode

    Design a logger system that receive stream of messages along with its timestamps, each message shoul ...

  9. Logger Rate Limiter 十秒限时计数器

    [抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...

随机推荐

  1. Errors reported here must be corrected before the service can be started

    场景: 安装.配置Apache24时候,最后会给出提示,如图:Errors reported here must be corrected before the service can be star ...

  2. 锐动SDK应用于在线教育方面的解决方案

    在线教育 PC端.Android端的屏幕.摄像头录制和直播功能,教师不再拘泥于专业的视频教室进行直播授课. 强大的视频编辑功能,便于课件的制作和不断修改升级. 在线课堂实现了教学视频内容在PC.PAD ...

  3. 【译】x86程序员手册28-7.7任务地址空间

    7.7 Task Address Space 任务地址空间 The LDT selector and PDBR fields of the TSS give software systems desi ...

  4. Qt 杂记——QTableWidget列表添加、删除(备份)

    1.列表的添加 需求:向一个有两列的Table中添加一条数据 思路:新建一个inputDialog,通过按钮打开Qt自带的inputDialog,传递回输入的数据,再添加到列表中 界面: 代码: in ...

  5. 【windows】自动化测试持续集成(CI)环境部署

    1. 环境准备 1.1 我的环境 1.Win10 64位 2.JDK 1.8.0_121 3.Tomcat 7.0.92 4. Jenkins 2.24 5.SVN-Server 3.8.1 1.2 ...

  6. Jmeter之计数器

    如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现. 计数器(counter):允许用户创建一个在线程组之内都可以被引用的计数器. 计数器允许用户配置一个起点,一个最大值, ...

  7. java几种连接数据库的方法

    package bean; import java.sql.Connection;import java.sql.DriverManager; public class jdbcTest { //不同 ...

  8. windows10下win+R快速打开程序

    按下win+R进入运行窗口,输入应用程序名称按下回车键 即可打开该应用,若提示“windows找不到文件”,请看下一步 可以采用建立统一的目录管理,新建目录“F:/local/bin” 将新建目录的路 ...

  9. jQuey中的return false作用是什么?

    jQuey中的return false作用是什么?在众多的语句中都有return false的使用,当然对于熟悉它的开发者来说,当然是知根知底,知道此语句的作用,当然也就知道在什么时候使用此语句,不过 ...

  10. 充当别的mcu的外部存储器(51类)

    // 锁存地址 - STC12C5A60S2 reg [15:0]rAddr_51; //存放51单片机传过来的地址 读51地址寄存器 always @ (posedge MCLKout or neg ...