L933. 最近的请求次数 java deque

class RecentCounter {
    Queue<Integer> queue;
    public RecentCounter() {
      queue=new ArrayDeque<>();
    }
    
    public int ping(int t) {
        queue.offer(t);						// offer或add,offer不抛出异常			
        while(queue.peek()< t- 3000){		//peek 看头 
            queue.poll();					//poll 获取并去掉头,空则返回null
        }
        return queue.size();
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 = obj.ping(t);
 */

你可能感兴趣的:(java,开发语言)