(C++ 練習) 933. Number of Recent Calls

題目 :

Write a class RecentCounter to count recent requests.

It has only one method: ping(int t), where t represents some time in milliseconds.

Return the number of pings that have been made from 3000 milliseconds ago until now.

Any ping with time in [t - 3000, t] will count, including the current ping.

It is guaranteed that every call to ping uses a strictly larger value of t than before.

大意 :

這個題目只有一個method : ping(int t), 而 t 代表的是 3000ms 之前到現在的呼叫次數.

題目有個需求, 就是 tn 必須大於前一個的 tn-1

 1 class RecentCounter {
 2 public:
 3     RecentCounter() {}
 4 
 5     int ping(int t) {
 6         while (!q.empty() && t - q.front() > 3000) q.pop();
 7         q.push(t);
 8         return q.size();
 9     }
10 private:
11     queue<int> q;
12 };

 

posted on 2019-03-06 00:41  OO程式猿  阅读(169)  评论(0)    收藏  举报

导航