Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a21d50

Browse files
feat: solve No.232
1 parent d0ee54c commit 9a21d50

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# 232. Implement Queue using Stacks
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Stack, Design, Queue.
5+
- Similar Questions: Implement Stack using Queues.
6+
7+
## Problem
8+
9+
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (`push`, `peek`, `pop`, and `empty`).
10+
11+
Implement the `MyQueue` class:
12+
13+
14+
15+
- `void push(int x)` Pushes element x to the back of the queue.
16+
17+
- `int pop()` Removes the element from the front of the queue and returns it.
18+
19+
- `int peek()` Returns the element at the front of the queue.
20+
21+
- `boolean empty()` Returns `true` if the queue is empty, `false` otherwise.
22+
23+
24+
**Notes:**
25+
26+
27+
28+
- You must use **only** standard operations of a stack, which means only `push to top`, `peek/pop from top`, `size`, and `is empty` operations are valid.
29+
30+
- Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
31+
32+
33+
34+
Example 1:
35+
36+
```
37+
Input
38+
["MyQueue", "push", "push", "peek", "pop", "empty"]
39+
[[], [1], [2], [], [], []]
40+
Output
41+
[null, null, null, 1, 1, false]
42+
43+
Explanation
44+
MyQueue myQueue = new MyQueue();
45+
myQueue.push(1); // queue is: [1]
46+
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
47+
myQueue.peek(); // return 1
48+
myQueue.pop(); // return 1, queue is [2]
49+
myQueue.empty(); // return false
50+
```
51+
52+
53+
**Constraints:**
54+
55+
56+
57+
- `1 <= x <= 9`
58+
59+
- At most `100` calls will be made to `push`, `pop`, `peek`, and `empty`.
60+
61+
- All the calls to `pop` and `peek` are valid.
62+
63+
64+
65+
**Follow-up:** Can you implement the queue such that each operation is **amortized** `O(1)` time complexity? In other words, performing `n` operations will take overall `O(n)` time even if one of those operations may take longer.
66+
67+
68+
## Solution
69+
70+
```javascript
71+
72+
var MyQueue = function() {
73+
this.stack1 = [];
74+
this.stack2 = [];
75+
};
76+
77+
/**
78+
* @param {number} x
79+
* @return {void}
80+
*/
81+
MyQueue.prototype.push = function(x) {
82+
this.stack1.push(x);
83+
};
84+
85+
/**
86+
* @return {number}
87+
*/
88+
MyQueue.prototype.pop = function() {
89+
if (this.stack2.length === 0) {
90+
while (this.stack1.length) this.stack2.push(this.stack1.pop());
91+
}
92+
return this.stack2.pop();
93+
};
94+
95+
/**
96+
* @return {number}
97+
*/
98+
MyQueue.prototype.peek = function() {
99+
if (this.stack2.length === 0) {
100+
while (this.stack1.length) this.stack2.push(this.stack1.pop());
101+
}
102+
return this.stack2[this.stack2.length - 1];
103+
};
104+
105+
/**
106+
* @return {boolean}
107+
*/
108+
MyQueue.prototype.empty = function() {
109+
return this.stack1.length === 0 && this.stack2.length === 0;
110+
};
111+
112+
/**
113+
* Your MyQueue object will be instantiated and called as such:
114+
* var obj = new MyQueue()
115+
* obj.push(x)
116+
* var param_2 = obj.pop()
117+
* var param_3 = obj.peek()
118+
* var param_4 = obj.empty()
119+
*/
120+
```
121+
122+
**Explain:**
123+
124+
nope.
125+
126+
**Complexity:**
127+
128+
* Time complexity : O(n).
129+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /