class RecentCounter {
Queue<Integer> queue;
public RecentCounter() {
queue = new LinkedList<>(); //创建队列
}
public int ping(int t) {
queue.add(t); //在队列中加入时间点
while(!queue.isEmpty() && t - queue.peek() > 3000){ //queue.peek()队列中最早进入的元素
queue.poll(); //用t减去最早进入元素 如果大于3000 就将最早元素删掉
}//持续循环 最总将队列中首尾元素之查控制在3000以内
return queue.size();//返回队列长度
}
}
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/