359. Logger Rate Limiter
做题历程:
- 本题应该做了不止一次,本次独立解出,大概耗时8分钟
难度很低,就是用一个Hashmap记录每个message的时间即可,然后不断更新。。
代码如下:
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) {
auto found = log_map.find(message);
if (found == log_map.end()) {
log_map[message] = timestamp;
return true;
}
else {
if (timestamp - log_map[message] < 10) {
return false;
}
else {
log_map[message] = timestamp;
return true;
}
}
}
private:
unordered_map<string, int> log_map;
};
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* bool param_1 = obj.shouldPrintMessage(timestamp,message);
*/