Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

359. Logger Rate Limiter #141

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

public class Logger { private Map<String, Integer> map; /* Initialize your data structure here. / public Logger() { map = new HashMap<String, Integer>(); }

/** 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;
    } 
    if(map.get(message) + 10 <= timestamp) {
        map.put(message, timestamp);
        return true;
    }

    return false;
}

}

/**

Shawngbk commented 7 years ago

google