zoukankan      html  css  js  c++  java
  • 359. Logger Rate Limiter

    题目:

    Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

    Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.

    It is possible that several messages arrive roughly at the same time.

    Example:

    Logger logger = new Logger();
    
    // logging string "foo" at timestamp 1
    logger.shouldPrintMessage(1, "foo"); returns true; 
    
    // logging string "bar" at timestamp 2
    logger.shouldPrintMessage(2,"bar"); returns true;
    
    // logging string "foo" at timestamp 3
    logger.shouldPrintMessage(3,"foo"); returns false;
    
    // logging string "bar" at timestamp 8
    logger.shouldPrintMessage(8,"bar"); returns false;
    
    // logging string "foo" at timestamp 10
    logger.shouldPrintMessage(10,"foo"); returns false;
    
    // logging string "foo" at timestamp 11
    logger.shouldPrintMessage(11,"foo"); returns true;
    

     链接:

    https://leetcode.com/problems/logger-rate-limiter/#/description

    3/12/2017

     1 public class Logger {
     2     HashMap<String, Integer> h;
     3     /** Initialize your data structure here. */
     4     public Logger() {
     5         h = new HashMap<String, Integer>();
     6     }
     7     
     8     /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
     9         If this method returns false, the message will not be printed.
    10         The timestamp is in seconds granularity. */
    11     public boolean shouldPrintMessage(int timestamp, String message) {
    12         if (h.containsKey(message) && timestamp - h.get(message) < 10) return false;
    13         else {
    14             h.put(message, timestamp);
    15             return true;
    16         }
    17     }
    18 }
    19 
    20 /**
    21  * Your Logger object will be instantiated and called as such:
    22  * Logger obj = new Logger();
    23  * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
    24  */

    看别人的算法发现Java 8里Map居然有getOrDefault这个函数了,果然进化了哦

    解法不错

     1 public class Logger {
     2 
     3     private Map<String, Integer> ok = new HashMap<>();
     4 
     5     public boolean shouldPrintMessage(int timestamp, String message) {
     6         if (timestamp < ok.getOrDefault(message, 0))
     7             return false;
     8         ok.put(message, timestamp + 10);
     9         return true;
    10     }
    11 }

    其他可参考:

    https://discuss.leetcode.com/topic/48615/a-solution-that-only-keeps-part-of-the-messages

  • 相关阅读:
    1021 个位数统计 (15 分
    1020 月饼 (25 分)
    1019 数字黑洞 (20 分)
    1018 锤子剪刀布 (20 分)
    1017 A除以B (20 分)
    KMP 串的模式匹配 (25 分)
    11-散列4 Hashing
    11-散列3 QQ帐户的申请与登陆 (25 分)
    11-散列1 电话聊天狂人 (25 分)
    使用正则表达式验证手机号码合法性
  • 原文地址:https://www.cnblogs.com/panini/p/6540805.html
Copyright © 2011-2022 走看看