代码拉取完成,页面将自动刷新
// Time: O(1), amortized
// Space: O(k), k is the max number of printed messages in last 10 seconds
class Logger {
public:
/** Initialize your data structure here. */
Logger() {
}
/** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
bool shouldPrintMessage(int timestamp, string message) {
while (!dq_.empty() && dq_.front().first <= timestamp - 10) {
printed_.erase(dq_.front().second);
dq_.pop_front();
}
if (printed_.count(message)) {
return false;
}
dq_.emplace_back(timestamp, message);
printed_.emplace(message);
return true;
}
private:
deque<pair<int, string>> dq_;
unordered_set<string> printed_;
};
// Time: O(1)
// Space: O(n), n is the number of total unique messages
class Logger2 {
public:
/** Initialize your data structure here. */
Logger() {
}
/** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
bool shouldPrintMessage(int timestamp, string message) {
if (message_time_.count(message) &&
timestamp < message_time_[message] + 10) {
return false;
}
message_time_[message] = timestamp;
return true;
}
private:
unordered_map<string, int> message_time_;
};
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* bool param_1 = obj.shouldPrintMessage(timestamp,message);
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。