933-最近的请求次数
题目要求:
输入:
[“RecentCounter”, “ping”, “ping”, “ping”, “ping”]
[[], [1], [100], [3001], [3002]]
输出:
[null, 1, 2, 3, 3]
第一次做队列的题目,
## while嵌套if判断那么时间会超时
class RecentCounter {
Queue<Integer> que;
//初始化计数器
public RecentCounter() {
que= new LinkedList<>();
}
public int ping(int t) {
que.add(t);
while(!que.isEmpty()&&t-que.peek()>3000){
que.poll();
}
return que.size();
}
}