leecode 362. 敲击计数器

 

 

import java.util.LinkedList;
import java.util.Queue;

class HitCounter {
     private  Queue<Integer> q =  new LinkedList<Integer>();
    /** Initialize your data structure here. */
    HitCounter() {}

    /** Record a hit.
     @param timestamp - The current timestamp (in seconds granularity). */
    void hit(int timestamp) {
        q.offer(timestamp);
    }

    /** Return the number of hits in the past 5 minutes.
     @param timestamp - The current timestamp (in seconds granularity). */
    int getHits(int timestamp) {
        while (!q.isEmpty() && timestamp - q.peek() >= 300) {
            q.poll();
        }
        return q.size();
    }
}

 

posted @ 2021-04-14 14:21  kpwong  阅读(72)  评论(0编辑  收藏  举报