933. 最近的请求次数
为保证权益,题目请参考 933. 最近的请求次数(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <queue>
using namespace std;
class RecentCounter
{
queue<int> que;
public:
RecentCounter()
{
}
int ping(int t)
{
que.push(t);
// cout << que.front() << endl;
while (que.front() < t - 3000)
{
que.pop();
}
return que.size();
}
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/
int main()
{
RecentCounter *obj = new RecentCounter();
cout << obj->ping(1) << endl;
cout << obj->ping(10) << endl;
cout << obj->ping(3001) << endl;
cout << obj->ping(3002) << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40