https://leetcode-cn.com/problems/number-of-recent-calls/
思路:搞一个队列,每次在新的 t t t值入队前,循环判断一下队首元素,把 < t − 3000 <t-3000 <t−3000的值都弹出去,然后返回队列的大小即可。
class RecentCounter {
public:
RecentCounter(){
}
int ping(int t) {
int val=t-3000;
while(!q.empty()&&q.front()<val)
q.pop();
q.push(t);
return q.size();
}
private:
queue<int> q;
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/