Problem:

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.

Summary:

设计一个日志系统,判断信息流中的信息能否在特定时间发送。每次传入一个字符串以及发送时间,若该字符串在前十秒未发送过,则return true,否则return false,不发送该字符串。

Solution:

1. unordered_map中存入上次某字符串的发送时间。

 class Logger {
public:
Logger() { } bool shouldPrintMessage(int timestamp, string message) {
if (!m.count(message) || timestamp - m[message] >= ) {
m[message] = timestamp;
return true;
}
else {
return false;
}
} private:
unordered_map<string, int> m;
};

2. 简便写法:unordered_map存入某字符串下次可发送时间。

 class Logger {
public:
Logger() { } bool shouldPrintMessage(int timestamp, string message) {
if (m[message] > timestamp) {
return false;
} m[message] = timestamp + ;
return true;
} private:
unordered_map<string, int> m;
};

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] 359. Logger Rate Limiter 记录速率限制器

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

  3. 359. Logger Rate Limiter

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

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

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

  5. [LC] 359. Logger Rate Limiter

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

  6. [LeetCode] 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. Sqlserver内置函数实现MD5

    16位: SELECT substring(sys.fn_sqlvarbasetostr(HashBytes('MD5', '需要加密字符串')),3,16) 32位 SELECT substring ...

  2. root与普通用户的切换

    普通用户切换到root用户:sudo su - root用户切换到普通用户:su henie

  3. MessageDialog

    var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components unavailable&qu ...

  4. Dubbo

    启用dubbo访问日志的方法 dubbo.protocol.accesslog=true dubbo.application.logger=slf4j 然后配置logger <logger na ...

  5. Sublime Text编辑器的12个技巧和诀窍

    本文为您提供Sublime Text编辑器的12个技巧和诀窍,深入挖掘这个看似简洁的代码编辑器,背后所隐藏的实现各种高级功能的无限可能. 1) 选择 以下是一些Sublime Text选择文本的快捷键 ...

  6. System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本问题

    出错的原因: 1.虽然报的是需要安装客户端8.1.7及以上版本,实际是由于.NET账户没有访问Oracle\bin文件夹的权限 2.在 Windows Server 2003/2008 或Window ...

  7. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  8. linux tomcat 启动

    在tomcat bin目录下启动时报权限不够 在bin目录下输入:chmod u+x *.sh 可解决 查看tomcat 是否关闭 ps -ef|grep java 例如显示如下 说明还没关闭root ...

  9. MyEclipse10--的使用经验

    MyEclipse10--的使用经验总结 ------------------ 1.MyEclipse中的验证validation----->>用MyEclipse做ExtJs项目研发的时 ...

  10. asp.net反向代理

    https://www.codeproject.com/Articles/18490/Reverse-Proxy-in-C-NET-v https://www.codeproject.com/Arti ...