LeetCode 359 Logger Rate Limiter
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的更多相关文章
- LeetCode 359. Logger Rate Limiter (记录速率限制器)$
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] 359. Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- 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 记录速率限制器
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 ...
随机推荐
- bzoj1415[NOI2005]聪聪和可可-期望的线性性
这道题之前我写过一个巨逗比的写法(传送门:http://www.cnblogs.com/liu-runda/p/6220381.html) 当时的原因是这道题可以抽象出和"绿豆蛙的归宿&qu ...
- Pitfalls: C++ 中的index运算符
名可名, 非常名 在调一个题目的过程中发现了一个之前从未意识到的问题. 考虑如下代码 vector<int> a; int f(int x){ a.push_back(x); return ...
- 不要遍历dom
function selectProvince() { $.ajax( { type: "post", url: "/province/getStrType", ...
- thinkphp访问不存在的模块或者方法跳转到404页面
使用的thinkphp 版本是3.2.0, 在config.php中配置 404地址,即可: 'TMPL_EXCEPTION_FILE' => './Application/Home/View/ ...
- 二维码相关---java生成二维码名片,并且自动保存到手机通讯录中...
http://blog.csdn.net/lidew521/article/details/24441825
- Java数据结构——平衡二叉树的平衡因子(转自牛客网)
若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...
- php function集合
/*更新商品的某个字段*/ function update_goods($goods_id, $field, $value) { if ($goods_id) { /* 清除缓存 */ clear_c ...
- WdatePicker 使用
限制范围为今年之后的3年 $("input[name='year']").attr("onClick","WdatePicker({dateFmt:' ...
- 锁(MySQL篇)—之MyISAM表锁
前言 锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是 ...
- Eclispe远程调试tomcat设置
首先在catelina.sh中添加 JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=23787,server=y,su ...