C++ solution using unordered_map


  • 0
    M
    class Logger {
    public:
        /** Initialize your data structure here. */
        unordered_map<string, int> myMap;
        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) {
            unordered_map<string, int>::iterator mapIt = myMap.find(message);
            if(mapIt == myMap.end()){
                myMap.insert(make_pair(message, timestamp));
                return true;
            }else{
                if((timestamp - mapIt->second) >= 10){
                    myMap.erase(mapIt);
                    myMap.insert(make_pair(message, timestamp));
                    return true;
                }else{
                    return false;
                }
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.