Leetcode - Logger Rate Limiter

My code:

import java.util.concurrent.ConcurrentHashMap;
public class Logger {
    ConcurrentHashMap map = new ConcurrentHashMap();
    /** Initialize your data structure here. */
    public 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. */
    public boolean shouldPrintMessage(int timestamp, String message) {
        if (!map.containsKey(message)) {
            map.put(message, timestamp);
            return true;
        }
        else {
            int ts = map.get(message);
            if (timestamp - ts < 10) {
                return false;
            }
            else {
                map.put(message, timestamp);
                return true;
            }
        }
    }
}

/**
 * Your Logger object will be instantiated and called as such:
 * Logger obj = new Logger();
 * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
 */

并不难。。。
然后觉得应该用 ConcurrentHashMap 确保同步性。

Anyway, Good luck, Richardo! -- 09/12/2016

你可能感兴趣的:(Leetcode - Logger Rate Limiter)