Java - No brainer using Hashmap


  • 1
    T
    public class Logger {
    
        /** Initialize your data structure here. */
        Map<String,Integer> map;
        public Logger() {
            map = new HashMap<>();
        }
        
        /** 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. */
        public boolean shouldPrintMessage(int timestamp, String message) {
            if(!map.containsKey(message)){
                map.put(message,timestamp);
                return true;
            }else{
                int ts = map.get(message);
                if(Math.abs(timestamp - ts) >=10){
                    map.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);
     */
    

Log in to reply
 

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