Leetcode-933 Number of Recent Calls(最近的请求次数)

 1 class RecentCounter
 2 {
 3     private:
 4         queue<int> q;
 5     public:
 6         RecentCounter()
 7         {
 8         }
 9 
10         int ping(int t)
11         {
12             if(q.empty())
13             {
14                 q.push(t);
15                 return 1;
16             }
17             if(t-q.front()<=3000)
18             {
19                 q.push(t);
20             }
21             else
22             {
23                 q.pop();
24                 while(!q.empty() && t-q.front()>3000)
25                 {
26                     q.pop();
27                 }
28                 if(q.empty())
29                 {
30                     q.push(t);
31                     return 1;
32                 }
33                 q.push(t);
34             }
35             return q.size();
36         }
37 };

 

posted @ 2018-11-04 12:36  Asurudo  阅读(352)  评论(0编辑  收藏  举报