博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] 359. Logger Rate Limiter 解题报告
阅读量:3676 次
发布时间:2019-05-21

本文共 1810 字,大约阅读时间需要 6 分钟。

题目链接: https://leetcode.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 1logger.shouldPrintMessage(1, "foo"); returns true; // logging string "bar" at timestamp 2logger.shouldPrintMessage(2,"bar"); returns true;// logging string "foo" at timestamp 3logger.shouldPrintMessage(3,"foo"); returns false;// logging string "bar" at timestamp 8logger.shouldPrintMessage(8,"bar"); returns false;// logging string "foo" at timestamp 10logger.shouldPrintMessage(10,"foo"); returns false;// logging string "foo" at timestamp 11logger.shouldPrintMessage(11,"foo"); returns true;

思路: 啰嗦了那么多都没用, 一个hash表就可以搞定.

代码如下:

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(hash.count(message) == 0 || timestamp -hash[message] >= 10)        {            hash[message] = timestamp;            return true;        }        return false;    }private:    unordered_map
hash;};/** * Your Logger object will be instantiated and called as such: * Logger obj = new Logger(); * bool param_1 = obj.shouldPrintMessage(timestamp,message); */

转载地址:http://uglbn.baihongyu.com/

你可能感兴趣的文章
二进制、十进制、十六进制之间转换
查看>>
BigDecimal加减乘除及比较大小
查看>>
int加char会报错???
查看>>
java内部类使用
查看>>
System.arraycopy()方法
查看>>
JAVA 基础学习之 数组加强和二位数组
查看>>
JAVA 基础学习之 面向对象-类和对象
查看>>
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) (异或+规律)
查看>>
Codeforces Round #635 (Div. 2) D. Xenia and Colorful Gems(二分)
查看>>
Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) B. High Load(树的直径+贪心)
查看>>
Codeforces Round #638 (Div. 2) D. Phoenix and Science(数学+思维)
查看>>
Codeforces Round #576 (Div. 1) C. Matching vs Independent Set(思维好题)
查看>>
Codeforces Round #639 (Div. 2) D. Monopole Magnets(bfs+模拟)(恶心的阅读理解题)
查看>>
一分钟搞懂与、或、非、异或优先级!
查看>>
CodeBlocks无法编译的解决方案
查看>>
关于ZigBee的学习笔记1.0
查看>>
秒懂!用通俗的话讲解“ZigBee终端节点入网”过程
查看>>
完美解决:Ubuntu 12.04右键没有打开终端选项
查看>>
快速理解:memmove和memcopy的区别
查看>>
strsep函数详解
查看>>