1. 程式人生 > >933. 最近的請求次數 (佇列 難度2) - 詳細題解

933. 最近的請求次數 (佇列 難度2) - 詳細題解

題目連結
在這裡插入圖片描述
這道題開始沒看懂…輸入輸出也不明確, 後來才明白, 其實就用一個佇列篩選一下就行了

class RecentCounter {
public:
    RecentCounter() {
        
    }
    
    int ping(int t) {
        //calculate the count of ping between t-3000 and t
        this->times.push(t);
        while(!this->times.empty() && this->times.
front()<(t-3000)) this->times.pop(); return times.size(); } private: queue<int> times; }; /** * Your RecentCounter object will be instantiated and called as such: * RecentCounter* obj = new RecentCounter(); * int param_1 = obj->ping(t); */