class Logger {
public:
Logger() {
}
bool shouldPrintMessage(int timestamp, string message) {
}
};
/**
* Your Logger object will be instantiated and called as such:
* Logger* obj = new Logger();
* bool param_1 = obj->shouldPrintMessage(timestamp,message);
*/
type Logger struct {
m map[string] int
}
/** Initialize your data structure here. */
func Constructor() Logger {
return Logger{m:map[string]int{}}
}
/** 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. */
func (this *Logger) ShouldPrintMessage(timestamp int, message string) bool {
res := false
if ts, ok := this.m[message]; !ok{
res = true
this.m[message] = timestamp
}else{
if timestamp - ts >= 10{
res = true
this.m[message] = timestamp
}
}
return res
}
/**
* Your Logger object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.ShouldPrintMessage(timestamp,message);
*/
from collections import deque
class Logger:
def __init__(self):
"""
Initialize your data structure here.
"""
self._msg_set = set()
self._msg_queue = deque()
def shouldPrintMessage(self, timestamp, message):
"""
Returns true if the message should be printed in the given timestamp, otherwise returns false.
"""
while self._msg_queue:
msg, ts = self._msg_queue[0]
if timestamp - ts >= 10:
self._msg_queue.popleft()
self._msg_set.remove(msg)
else:
break
if message not in self._msg_set:
self._msg_set.add(message)
self._msg_queue.append((message, timestamp))
return True
else:
return False
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)
class Logger:
def __init__(self):
"""
Initialize your data structure here.
"""
self._msg_dict = {}
def shouldPrintMessage(self, timestamp, message):
"""
Returns true if the message should be printed in the given timestamp, otherwise returns false.
"""
if message not in self._msg_dict:
# case 1). add the message to print
self._msg_dict[message] = timestamp
return True
if timestamp - self._msg_dict[message] >= 10:
# case 2). update the timestamp of the message
self._msg_dict[message] = timestamp
return True
else:
return False
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)
class Logger {
private HashMap<String, Integer> msgDict;
/** Initialize your data structure here. */
public Logger() {
msgDict = new HashMap<String, Integer>();
}
/**
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
*/
public boolean shouldPrintMessage(int timestamp, String message) {
if (!this.msgDict.containsKey(message)) {
this.msgDict.put(message, timestamp);
return true;
}
Integer oldTimestamp = this.msgDict.get(message);
if (timestamp - oldTimestamp >= 10) {
this.msgDict.put(message, timestamp);
return true;
} else
return false;
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
class Pair<U, V> {
public U first;
public V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
}
class Logger {
private LinkedList<Pair<String, Integer>> msgQueue;
private HashSet<String> msgSet;
/** Initialize your data structure here. */
public Logger() {
msgQueue = new LinkedList<Pair<String, Integer>>();
msgSet = new HashSet<String>();
}
/**
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
*/
public boolean shouldPrintMessage(int timestamp, String message) {
// clean up.
while (msgQueue.size() > 0) {
Pair<String, Integer> head = msgQueue.getFirst();
if (timestamp - head.second >= 10) {
msgQueue.removeFirst();
msgSet.remove(head.first);
} else
break;
}
if (!msgSet.contains(message)) {
Pair<String, Integer> newEntry = new Pair<String, Integer>(message, timestamp);
msgQueue.addLast(newEntry);
msgSet.add(message);
return true;
} else
return false;
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/