Leetcode - Logger Rate Limiter

My code:

import java.util.concurrent.ConcurrentHashMap;
public class Logger {
    ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
    /** 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

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/dd39fba3d35a#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞