zoukankan      html  css  js  c++  java
  • [LeetCode] 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;

    日志速率限制器。请你设计一个日志系统,可以流式接收日志以及它的时间戳。该日志会被打印出来,需要满足一个条件:当且仅当日志内容 在过去的 10 秒钟内没有被打印过。给你一条日志的内容和它的时间戳(粒度为秒级),如果这条日志在给定的时间戳应该被打印出来,则返回 true,否则请返回 false。要注意的是,可能会有多条日志在同一时间被系统接收。

    思路是用hashmap记录日志内容和时间,每次看到有相同日志进来,则查看一下上一个timestamp是否在10秒钟以内。

    Java实现

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

    JavaScript实现

     1 /**
     2  * Initialize your data structure here.
     3  */
     4 var Logger = function() {
     5     this.map = new Map();
     6 };
     7 
     8 /**
     9  * Returns true if the message should be printed in the given timestamp, otherwise returns false.
    10         If this method returns false, the message will not be printed.
    11         The timestamp is in seconds granularity. 
    12  * @param {number} timestamp 
    13  * @param {string} message
    14  * @return {boolean}
    15  */
    16 Logger.prototype.shouldPrintMessage = function(timestamp, message) {
    17     return (this.map.has(message) && timestamp - this.map.get(message) < 10) ? false : this.map.set(message, timestamp);
    18 };
    19 
    20 /** 
    21  * Your Logger object will be instantiated and called as such:
    22  * var obj = new Logger()
    23  * var param_1 = obj.shouldPrintMessage(timestamp,message)
    24  */

    LeetCode 题目总结

  • 相关阅读:
    5款强大的Java Web开发工具
    [Visual Studio] 重置默认设置 还原默认设置
    [Visual Studio] VS2012调试时很慢的解决方案
    SQL 语句与性能之执行顺序
    SQL 语句与性能之联合查询和联合分类查询
    使用SHFB(Sandcastle Help File Builder)建立MSDN风格的代码文档
    循序渐进地代码重构
    博客收藏
    [已解决]:调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "c:WindowsMicrosoft.NETFrameworkv4.0.30319\aspnet_filter.
    [Visual Studio] .vsix项目模板制作
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12695414.html
Copyright © 2011-2022 走看看