Leetcode:logger Rate Limiter

Source: Internet
Author: User

Design a logger system this receive stream of messages along with its timestamps, each message should be printedifand onlyifIt isn't printed in the last 10seconds. Given a message and a timestamp (in seconds granularity),return true ifThe message should is printed in the given timestamp, otherwise returnsfalse. It is possible this several messages arrive roughly at the same time. Example:logger Logger=NewLogger ();//Logging string "foo" at timestamp 1Logger.shouldprintmessage (1, "foo"); Returnstrue; //Logging string "Bar" at timestamp 2Logger.shouldprintmessage (2, "bar"); Returnstrue;//Logging string "foo" at timestamp 3Logger.shouldprintmessage (3, "foo"); Returnsfalse;//Logging string "Bar" at timestamp 8Logger.shouldprintmessage (8, "bar"); Returnsfalse;//Logging string "foo" at timestampLogger.shouldprintmessage (Ten, "Foo"); Returnsfalse;//Logging string "foo" at timestampLogger.shouldprintmessage (One, "foo"); Returnstrue;

HashMap

1  Public classLogger {2Hashmap<string, integer>map;3     4     /**Initialize your data structure here.*/5      PublicLogger () {6Map =NewHashmap<string, integer>();7     }8     9     /**Returns True if the message should is printed in the given timestamp, otherwise Returns false.Ten If This method returns false, the message won't be printed. One The timestamp is in seconds granularity.*/ A      Public BooleanShouldprintmessage (inttimestamp, String message) { -         if(!map.containskey (message) | | | timestamp >=map.get (message)) { -Map.put (Message, timestamp+10); the             return true; -         } -         return false; -     } + } -  + /** A * Your Logger object would be instantiated and called as such: at * Logger obj = new Logger (); - * Boolean param_1 = Obj.shouldprintmessage (timestamp,message); -  */

Leetcode:logger Rate Limiter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.