Skip to content

Commit 8a1a0ea

Browse files
committed
feat: add solutions to lc problem: No.0232
No.0232.Implement Queue using Stacks
1 parent 42963f4 commit 8a1a0ea

File tree

8 files changed

+443
-202
lines changed

8 files changed

+443
-202
lines changed

solution/0200-0299/0218.The Skyline Problem/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
1010
city, n = 0, len(buildings)
1111
for line in lines:
1212
while city < n and buildings[city][0] <= line:
13-
pq.put([-buildings[city][2], buildings[city]
14-
[0], buildings[city][1]])
13+
pq.put([-buildings[city][2], buildings[city][0], buildings[city][1]])
1514
city += 1
1615
while not pq.empty() and pq.queue[0][2] <= line:
1716
pq.get()

solution/0200-0299/0232.Implement Queue using Stacks/README.md

+156-64
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ myQueue.empty(); // return false
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:双栈**
73+
74+
使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
75+
76+
入队时,直接将元素入栈 `stk1`。时间复杂度 $O(1)$。
77+
78+
出队时,先判断栈 `stk2` 是否为空,如果为空,则将栈 `stk1` 中的元素全部出栈并入栈 `stk2`,然后再从栈 `stk2` 中出栈一个元素。如果栈 `stk2` 不为空,则直接从栈 `stk2` 中出栈一个元素。均摊时间复杂度 $O(1)$。
79+
80+
获取队首元素时,先判断栈 `stk2` 是否为空,如果为空,则将栈 `stk1` 中的元素全部出栈并入栈 `stk2`,然后再从栈 `stk2` 中获取栈顶元素。如果栈 `stk2` 不为空,则直接从栈 `stk2` 中获取栈顶元素。均摊时间复杂度 $O(1)$。
81+
82+
判断队列是否为空时,只要判断两个栈是否都为空即可。时间复杂度 $O(1)$。
83+
7284
<!-- tabs:start -->
7385

7486
### **Python3**
@@ -78,45 +90,27 @@ myQueue.empty(); // return false
7890
```python
7991
class MyQueue:
8092
def __init__(self):
81-
"""
82-
Initialize your data structure here.
83-
"""
84-
self.s1 = []
85-
self.s2 = []
93+
self.stk1 = []
94+
self.stk2 = []
8695

8796
def push(self, x: int) -> None:
88-
"""
89-
Push element x to the back of queue.
90-
"""
91-
self.s1.append(x)
97+
self.stk1.append(x)
9298

9399
def pop(self) -> int:
94-
"""
95-
Removes the element from in front of queue and returns that element.
96-
"""
97-
self._move()
98-
return self.s2.pop()
100+
self.move()
101+
return self.stk2.pop()
99102

100103
def peek(self) -> int:
101-
"""
102-
Get the front element.
103-
"""
104-
self._move()
105-
return self.s2[-1]
104+
self.move()
105+
return self.stk2[-1]
106106

107107
def empty(self) -> bool:
108-
"""
109-
Returns whether the queue is empty.
110-
"""
111-
return len(self.s1) + len(self.s2) == 0
108+
return not self.stk1 and not self.stk2
112109

113-
def _move(self):
114-
"""
115-
Move elements from s1 to s2.
116-
"""
117-
if len(self.s2) == 0:
118-
while len(self.s1) > 0:
119-
self.s2.append(self.s1.pop())
110+
def move(self):
111+
if not self.stk2:
112+
while self.stk1:
113+
self.stk2.append(self.stk1.pop())
120114

121115

122116
# Your MyQueue object will be instantiated and called as such:
@@ -133,41 +127,35 @@ class MyQueue:
133127

134128
```java
135129
class MyQueue {
130+
private Deque<Integer> stk1 = new ArrayDeque<>();
131+
private Deque<Integer> stk2 = new ArrayDeque<>();
136132

137-
private Deque<Integer> s1 = new ArrayDeque<>();
138-
private Deque<Integer> s2 = new ArrayDeque<>();
139-
140-
/** Initialize your data structure here. */
141133
public MyQueue() {
134+
142135
}
143136

144-
/** Push element x to the back of queue. */
145137
public void push(int x) {
146-
s1.push(x);
138+
stk1.push(x);
147139
}
148140

149-
/** Removes the element from in front of queue and returns that element. */
150141
public int pop() {
151142
move();
152-
return s2.pop();
143+
return stk2.pop();
153144
}
154145

155-
/** Get the front element. */
156146
public int peek() {
157147
move();
158-
return s2.peek();
148+
return stk2.peek();
159149
}
160150

161-
/** Returns whether the queue is empty. */
162151
public boolean empty() {
163-
return s1.isEmpty() && s2.isEmpty();
152+
return stk1.isEmpty() && stk2.isEmpty();
164153
}
165154

166-
/** Move elements from s1 to s2. */
167155
private void move() {
168-
if (s2.isEmpty()) {
169-
while (!s1.isEmpty()) {
170-
s2.push(s1.pop());
156+
while (stk2.isEmpty()) {
157+
while (!stk1.isEmpty()) {
158+
stk2.push(stk1.pop());
171159
}
172160
}
173161
}
@@ -183,41 +171,145 @@ class MyQueue {
183171
*/
184172
```
185173

174+
### **C++**
175+
176+
```cpp
177+
class MyQueue {
178+
public:
179+
MyQueue() {
180+
}
181+
182+
void push(int x) {
183+
stk1.push(x);
184+
}
185+
186+
int pop() {
187+
move();
188+
int ans = stk2.top();
189+
stk2.pop();
190+
return ans;
191+
}
192+
193+
int peek() {
194+
move();
195+
return stk2.top();
196+
}
197+
198+
bool empty() {
199+
return stk1.empty() && stk2.empty();
200+
}
201+
202+
private:
203+
stack<int> stk1;
204+
stack<int> stk2;
205+
206+
void move() {
207+
if (stk2.empty()) {
208+
while (!stk1.empty()) {
209+
stk2.push(stk1.top());
210+
stk1.pop();
211+
}
212+
}
213+
}
214+
};
215+
216+
/**
217+
* Your MyQueue object will be instantiated and called as such:
218+
* MyQueue* obj = new MyQueue();
219+
* obj->push(x);
220+
* int param_2 = obj->pop();
221+
* int param_3 = obj->peek();
222+
* bool param_4 = obj->empty();
223+
*/
224+
```
225+
226+
### **Go**
227+
228+
```go
229+
type MyQueue struct {
230+
stk1 []int
231+
stk2 []int
232+
}
233+
234+
func Constructor() MyQueue {
235+
return MyQueue{[]int{}, []int{}}
236+
}
237+
238+
func (this *MyQueue) Push(x int) {
239+
this.stk1 = append(this.stk1, x)
240+
}
241+
242+
func (this *MyQueue) Pop() int {
243+
this.move()
244+
ans := this.stk2[len(this.stk2)-1]
245+
this.stk2 = this.stk2[:len(this.stk2)-1]
246+
return ans
247+
}
248+
249+
func (this *MyQueue) Peek() int {
250+
this.move()
251+
return this.stk2[len(this.stk2)-1]
252+
}
253+
254+
func (this *MyQueue) Empty() bool {
255+
return len(this.stk1) == 0 && len(this.stk2) == 0
256+
}
257+
258+
func (this *MyQueue) move() {
259+
if len(this.stk2) == 0 {
260+
for len(this.stk1) > 0 {
261+
this.stk2 = append(this.stk2, this.stk1[len(this.stk1)-1])
262+
this.stk1 = this.stk1[:len(this.stk1)-1]
263+
}
264+
}
265+
}
266+
267+
/**
268+
* Your MyQueue object will be instantiated and called as such:
269+
* obj := Constructor();
270+
* obj.Push(x);
271+
* param_2 := obj.Pop();
272+
* param_3 := obj.Peek();
273+
* param_4 := obj.Empty();
274+
*/
275+
```
276+
186277
### **TypeScript**
187278

188279
```ts
189280
class MyQueue {
190-
stack1: number[];
191-
stack2: number[];
281+
stk1: number[];
282+
stk2: number[];
283+
192284
constructor() {
193-
this.stack1 = [];
194-
this.stack2 = [];
285+
this.stk1 = [];
286+
this.stk2 = [];
195287
}
196288

197289
push(x: number): void {
198-
this.stack1.push(x);
290+
this.stk1.push(x);
199291
}
200292

201293
pop(): number {
202-
if (!this.stack2.length) {
203-
while (this.stack1.length) {
204-
this.stack2.push(this.stack1.pop());
205-
}
206-
}
207-
return this.stack2.pop();
294+
this.move();
295+
return this.stk2.pop();
208296
}
209297

210298
peek(): number {
211-
if (!this.stack2.length) {
212-
while (this.stack1.length) {
213-
this.stack2.push(this.stack1.pop());
214-
}
215-
}
216-
return this.stack2[this.stack2.length - 1];
299+
this.move();
300+
return this.stk2[this.stk2.length - 1];
217301
}
218302

219303
empty(): boolean {
220-
return !this.stack1.length && !this.stack2.length;
304+
return !this.stk1.length && !this.stk2.length;
305+
}
306+
307+
move(): void {
308+
if (!this.stk2.length) {
309+
while (this.stk1.length) {
310+
this.stk2.push(this.stk1.pop());
311+
}
312+
}
221313
}
222314
}
223315

0 commit comments

Comments
 (0)