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

  • 相关阅读:
    poj 3125 Printer Queue(STL注意事项)
    poj 2823 Sliding Window (STL超时版)
    poj 1088 滑雪 详解
    poj 2983 Is the Information Reliable?
    poj 2524 Ubiquitous Religions (STL与非STL的对比)
    高精度算法集合
    zTree v2.6 v3.0 初始化 / 方法对比
    下面是关于rownum的介绍(oracle)
    web性能优化
    jQueryEasyui,DataGrid几个常用的操作
  • 原文地址:https://www.cnblogs.com/panini/p/6540805.html
Copyright © 2011-2022 走看看