zoukankan      html  css  js  c++  java
  • 362. Design Hit Counter

    /*
     * 362. Design Hit Counter
     * 2016-7-15 by Mingyang
     * 这个题目意思很难懂,但其实本质上没什么东西
     */
    class HitCounter {
        public Queue<Integer> queue;
        /** Initialize your data structure here. */
        public HitCounter() {
            queue=new LinkedList();
        }    
        /** Record a hit.
            @param timestamp - The current timestamp (in seconds granularity). */
        public void hit(int timestamp) {
            queue.add(timestamp);
        }    
        /** Return the number of hits in the past 5 minutes.
            @param timestamp - The current timestamp (in seconds granularity). 301 和 1的差距刚好等于300,刚好出界 */
        public int getHits(int timestamp) {
            while(!queue.isEmpty()&&timestamp-queue.peek()>=300){
                queue.poll();
            }
            return queue.size();
        }
    }
  • 相关阅读:
    8-JS闭包、回调实例
    7-闭包、回调
    6-JS函数(二)
    5-JS函数
    4-JS对象
    3-WebPack
    2-Babel
    1-NPM
    25-React事件处理及条件渲染
    java初始化笔记
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5675359.html
Copyright © 2011-2022 走看看