362. Design Hit Counter

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();
    }
}