lurenaa的博客

😝实现

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {

}

/** Push element x to the back of queue. */
void push(int x) {
stk.push(x);
}

/** Removes the element from in front of queue and returns that element. */
int pop() {
while(stk.size() != 1) {
int x = stk.top();
stk.pop();
helper.push(x);
}
int x = stk.top();
stk.pop();
while(helper.size())
{
int x = helper.top();
helper.pop();
stk.push(x);
}
return x;
}

/** Get the front element. */
int peek() {
while(stk.size() != 1) {
int x = stk.top();
stk.pop();
helper.push(x);
}
int x = stk.top();
while(helper.size())
{
int x = helper.top();
helper.pop();
stk.push(x);
}
return x;
}

/** Returns whether the queue is empty. */
bool empty() {
return stk.size() == 0;
}
private:
stack<int > stk, helper;
};

Accepted

17/17 cases passed (0 ms)

Your runtime beats 100 % of cpp submissions

Your memory usage beats 5.28 % of cpp submissions (9.1 MB)