Skip to content

Commit 4a36977

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 03.04. 化栈为队
1 parent 3ce6b93 commit 4a36977

File tree

4 files changed

+366
-51
lines changed

4 files changed

+366
-51
lines changed

lcci/03.04.Implement Queue using Stacks/README.md

+100-1
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,119 @@
66

77
## 解法
88
<!-- 这里可写通用的实现逻辑 -->
9-
9+
- 每次压入元素时,放入第 1 个栈中;
10+
- 第 2 个栈不为空时,不能倒入元素;
11+
- 第 2 个栈为空时,必须将第 1 个栈的所有元素按顺序倒入第 2 个栈中。
1012

1113
### Python3
1214
<!-- 这里可写当前语言的特殊实现逻辑 -->
1315

1416
```python
17+
class MyQueue:
18+
19+
def __init__(self):
20+
"""
21+
Initialize your data structure here.
22+
"""
23+
self._s1, self._s2 = [], []
24+
25+
26+
def push(self, x: int) -> None:
27+
"""
28+
Push element x to the back of queue.
29+
"""
30+
self._s1.append(x)
31+
32+
33+
def pop(self) -> int:
34+
"""
35+
Removes the element from in front of queue and returns that element.
36+
"""
37+
if len(self._s2) == 0:
38+
while self._s1:
39+
self._s2.append(self._s1.pop())
40+
return self._s2.pop()
41+
1542

43+
def peek(self) -> int:
44+
"""
45+
Get the front element.
46+
"""
47+
if len(self._s2) == 0:
48+
while self._s1:
49+
self._s2.append(self._s1.pop())
50+
return self._s2[-1]
51+
52+
53+
def empty(self) -> bool:
54+
"""
55+
Returns whether the queue is empty.
56+
"""
57+
return len(self._s1) + len(self._s2) == 0
58+
59+
60+
61+
# Your MyQueue object will be instantiated and called as such:
62+
# obj = MyQueue()
63+
# obj.push(x)
64+
# param_2 = obj.pop()
65+
# param_3 = obj.peek()
66+
# param_4 = obj.empty()
1667
```
1768

1869
### Java
1970
<!-- 这里可写当前语言的特殊实现逻辑 -->
2071

2172
```java
73+
class MyQueue {
74+
private Stack<Integer> s1;
75+
private Stack<Integer> s2;
76+
77+
/** Initialize your data structure here. */
78+
public MyQueue() {
79+
s1 = new Stack<>();
80+
s2 = new Stack<>();
81+
}
82+
83+
/** Push element x to the back of queue. */
84+
public void push(int x) {
85+
s1.push(x);
86+
}
87+
88+
/** Removes the element from in front of queue and returns that element. */
89+
public int pop() {
90+
if (s2.empty()) {
91+
while (!s1.empty()) {
92+
s2.push(s1.pop());
93+
}
94+
}
95+
return s2.pop();
96+
}
97+
98+
/** Get the front element. */
99+
public int peek() {
100+
if (s2.empty()) {
101+
while (!s1.empty()) {
102+
s2.push(s1.pop());
103+
}
104+
}
105+
return s2.peek();
106+
}
107+
108+
/** Returns whether the queue is empty. */
109+
public boolean empty() {
110+
return s1.empty() && s2.empty();
111+
}
112+
}
22113

114+
/**
115+
* Your MyQueue object will be instantiated and called as such:
116+
* MyQueue obj = new MyQueue();
117+
* obj.push(x);
118+
* int param_2 = obj.pop();
119+
* int param_3 = obj.peek();
120+
* boolean param_4 = obj.empty();
121+
*/
23122
```
24123

25124
### ...
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,172 @@
1-
# [03.04. Implement Queue using Stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci)
2-
3-
## Description
4-
<p>Implement a MyQueue class which implements a queue using two stacks.</p>
5-
&nbsp;
6-
7-
<p><strong>Example: </strong></p>
8-
9-
<pre>
10-
MyQueue queue = new MyQueue();
11-
12-
queue.push(1);
13-
queue.push(2);
14-
queue.peek(); // return 1
15-
queue.pop(); // return 1
16-
queue.empty(); // return false</pre>
17-
18-
<p>&nbsp;</p>
19-
20-
<p><b>Notes:</b></p>
21-
22-
<ul>
23-
<li>You must use&nbsp;<i>only</i>&nbsp;standard operations of a stack -- which means only&nbsp;<code>push to top</code>,&nbsp;<code>peek/pop from top</code>,&nbsp;<code>size</code>, and&nbsp;<code>is empty</code>&nbsp;operations are valid.</li>
24-
<li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li>
25-
<li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li>
26-
</ul>
27-
28-
<p>&nbsp;</p>
29-
30-
31-
32-
## Solutions
33-
34-
35-
### Python3
36-
37-
```python
38-
39-
```
40-
41-
### Java
42-
43-
```java
44-
45-
```
46-
47-
### ...
48-
```
49-
50-
```
1+
# [03.04. Implement Queue using Stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci)
2+
3+
## Description
4+
<p>Implement a MyQueue class which implements a queue using two stacks.</p>
5+
6+
&nbsp;
7+
8+
9+
10+
<p><strong>Example: </strong></p>
11+
12+
13+
14+
<pre>
15+
16+
MyQueue queue = new MyQueue();
17+
18+
19+
20+
queue.push(1);
21+
22+
queue.push(2);
23+
24+
queue.peek(); // return 1
25+
26+
queue.pop(); // return 1
27+
28+
queue.empty(); // return false</pre>
29+
30+
31+
32+
<p>&nbsp;</p>
33+
34+
35+
36+
<p><b>Notes:</b></p>
37+
38+
39+
40+
<ul>
41+
42+
<li>You must use&nbsp;<i>only</i>&nbsp;standard operations of a stack -- which means only&nbsp;<code>push to top</code>,&nbsp;<code>peek/pop from top</code>,&nbsp;<code>size</code>, and&nbsp;<code>is empty</code>&nbsp;operations are valid.</li>
43+
44+
<li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li>
45+
46+
<li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li>
47+
48+
</ul>
49+
50+
51+
52+
<p>&nbsp;</p>
53+
54+
55+
56+
57+
## Solutions
58+
59+
60+
### Python3
61+
62+
```python
63+
class MyQueue:
64+
65+
def __init__(self):
66+
"""
67+
Initialize your data structure here.
68+
"""
69+
self._s1, self._s2 = [], []
70+
71+
72+
def push(self, x: int) -> None:
73+
"""
74+
Push element x to the back of queue.
75+
"""
76+
self._s1.append(x)
77+
78+
79+
def pop(self) -> int:
80+
"""
81+
Removes the element from in front of queue and returns that element.
82+
"""
83+
if len(self._s2) == 0:
84+
while self._s1:
85+
self._s2.append(self._s1.pop())
86+
return self._s2.pop()
87+
88+
89+
def peek(self) -> int:
90+
"""
91+
Get the front element.
92+
"""
93+
if len(self._s2) == 0:
94+
while self._s1:
95+
self._s2.append(self._s1.pop())
96+
return self._s2[-1]
97+
98+
99+
def empty(self) -> bool:
100+
"""
101+
Returns whether the queue is empty.
102+
"""
103+
return len(self._s1) + len(self._s2) == 0
104+
105+
106+
107+
# Your MyQueue object will be instantiated and called as such:
108+
# obj = MyQueue()
109+
# obj.push(x)
110+
# param_2 = obj.pop()
111+
# param_3 = obj.peek()
112+
# param_4 = obj.empty()
113+
```
114+
115+
### Java
116+
117+
```java
118+
class MyQueue {
119+
private Stack<Integer> s1;
120+
private Stack<Integer> s2;
121+
122+
/** Initialize your data structure here. */
123+
public MyQueue() {
124+
s1 = new Stack<>();
125+
s2 = new Stack<>();
126+
}
127+
128+
/** Push element x to the back of queue. */
129+
public void push(int x) {
130+
s1.push(x);
131+
}
132+
133+
/** Removes the element from in front of queue and returns that element. */
134+
public int pop() {
135+
if (s2.empty()) {
136+
while (!s1.empty()) {
137+
s2.push(s1.pop());
138+
}
139+
}
140+
return s2.pop();
141+
}
142+
143+
/** Get the front element. */
144+
public int peek() {
145+
if (s2.empty()) {
146+
while (!s1.empty()) {
147+
s2.push(s1.pop());
148+
}
149+
}
150+
return s2.peek();
151+
}
152+
153+
/** Returns whether the queue is empty. */
154+
public boolean empty() {
155+
return s1.empty() && s2.empty();
156+
}
157+
}
158+
159+
/**
160+
* Your MyQueue object will be instantiated and called as such:
161+
* MyQueue obj = new MyQueue();
162+
* obj.push(x);
163+
* int param_2 = obj.pop();
164+
* int param_3 = obj.peek();
165+
* boolean param_4 = obj.empty();
166+
*/
167+
```
168+
169+
### ...
170+
```
171+
172+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class MyQueue {
2+
private Stack<Integer> s1;
3+
private Stack<Integer> s2;
4+
5+
/** Initialize your data structure here. */
6+
public MyQueue() {
7+
s1 = new Stack<>();
8+
s2 = new Stack<>();
9+
}
10+
11+
/** Push element x to the back of queue. */
12+
public void push(int x) {
13+
s1.push(x);
14+
}
15+
16+
/** Removes the element from in front of queue and returns that element. */
17+
public int pop() {
18+
if (s2.empty()) {
19+
while (!s1.empty()) {
20+
s2.push(s1.pop());
21+
}
22+
}
23+
return s2.pop();
24+
}
25+
26+
/** Get the front element. */
27+
public int peek() {
28+
if (s2.empty()) {
29+
while (!s1.empty()) {
30+
s2.push(s1.pop());
31+
}
32+
}
33+
return s2.peek();
34+
}
35+
36+
/** Returns whether the queue is empty. */
37+
public boolean empty() {
38+
return s1.empty() && s2.empty();
39+
}
40+
}
41+
42+
/**
43+
* Your MyQueue object will be instantiated and called as such:
44+
* MyQueue obj = new MyQueue();
45+
* obj.push(x);
46+
* int param_2 = obj.pop();
47+
* int param_3 = obj.peek();
48+
* boolean param_4 = obj.empty();
49+
*/

0 commit comments

Comments
 (0)