题目地址:https://leetcode-cn.com/problems/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;

题目大意

请你设计一个日志系统,可以流式接收日志以及它的时间戳。

该日志会被打印出来,需要满足一个条件:当且仅当日志内容 在过去的 10 秒钟内没有被打印过。

给你一条日志的内容和它的时间戳(粒度为秒级),如果这条日志在给定的时间戳应该被打印出来,则返回 true,否则请返回 false。

要注意的是,可能会有多条日志在同一时间被系统接收。

解题方法

字典

使用一个字典,保存如果10秒内没有被打印过的消息的时间戳。每个消息到达的时候,查找是否在字典中出现过,如果时间戳的间隔小于10秒,则直接返回false.

C++代码如下:

class Logger {
public:
/** Initialize your data structure here. */
Logger() {
} /** 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. */
bool shouldPrintMessage(int timestamp, string message) {
if (m_.count(message) && timestamp - m_[message] < 10) {
return false;
}
m_[message] = timestamp;
return true;
}
private:
unordered_map<string, int> m_;
}; /**
* Your Logger object will be instantiated and called as such:
* Logger* obj = new Logger();
* bool param_1 = obj->shouldPrintMessage(timestamp,message);
*/

日期

2019 年 9 月 17 日 —— 听了hulu宣讲会,觉得hulu的压力不大

【LeetCode】359. Logger Rate Limiter 解题报告(C++)的更多相关文章

  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. 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. [LC] 359. Logger Rate Limiter

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

  6. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. distmat 计算遗传距离

    distmat 可用于计算遗传距离,得到距离矩阵 1 在线运算 可通过在线进行遗传距离的计算,网址:http://www.bioinformatics.nl/cgi-bin/emboss/distma ...

  2. linux中的颜色

    echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033 ...

  3. R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)

    转载:http://blog.csdn.net/duqi_yc/article/details/9817243 目录 Table of Contents 1 正则表达式简介 2 字符数统计和字符翻译 ...

  4. Qemu/kvm虚拟化源码解析学习视频资料

    地址链接:tao宝搜索:Linux云计算KVM Qemu虚拟化视频源码讲解+实践​https://item.taobao.com/item.htm?ft=t&id=646300730262 L ...

  5. 生产调优3 HDFS-多目录配置

    目录 HDFS-多目录配置 NameNode多目录配置 1.修改hdfs-site.xml 2.格式化NameNode DataNode多目录配置(重要) 1.修改hdfs-site.xml 2.测试 ...

  6. 浅讲.Net 6 之 WebApplicationBuilder

    介绍 .Net 6为我们带来的一种全新的引导程序启动的方式.与之前的拆分成Program.cs和Startup不同,整个引导启动代码都在Program.cs中. WebApplicationBuild ...

  7. 「Spark从精通到重新入门(一)」Spark 中不可不知的动态优化

    前言 Apache Spark 自 2010 年面世,到现在已经发展为大数据批计算的首选引擎.而在 2020 年 6 月份发布的Spark 3.0 版本也是 Spark 有史以来最大的 Release ...

  8. adverb

    An adverb is a word or an expression that modifies a verb, adjective, another adverb, determiner [限定 ...

  9. tomcat拦截特殊字符报400,如 "|" "{" "}" ","等符号的解决方案

    最近在做一个项目,需要对外暴露两个接口接收别人给的参数,但是有一个问题就是对方的项目是一个老项目,在传参数的时候是将多个字符放在一个参数里面用"|"进行分割,然而他们传参数的时候又 ...

  10. css相关,flex布局全通!

    寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平.垂直同时居中. 记得刚开始学习 CSS 的时候,看到 float 属性不由得感觉眼前一亮,顺理成章的联想到 Word 文 ...