Skip to content

Commit 96911f0

Browse files
committedFeb 13, 2022
feat: update solutions to lcof problem: No.09
面试题09.用两个栈实现队列
1 parent e4e3985 commit 96911f0

File tree

9 files changed

+209
-198
lines changed

9 files changed

+209
-198
lines changed
 

‎lcof/面试题09. 用两个栈实现队列/README.md

+81-83
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,40 @@
22

33
## 题目描述
44

5-
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 `appendTail``deleteHead` ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,`deleteHead`  操作返回 -1 )
5+
<p>用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 <code>appendTail</code><code>deleteHead</code> ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,<code>deleteHead</code>&nbsp;操作返回 -1 )</p>
66

7-
**示例 1:**
7+
<p>&nbsp;</p>
88

9-
```
10-
输入:
11-
["CQueue","appendTail","deleteHead","deleteHead"]
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre><strong>输入:</strong>
12+
[&quot;CQueue&quot;,&quot;appendTail&quot;,&quot;deleteHead&quot;,&quot;deleteHead&quot;]
1213
[[],[3],[],[]]
13-
输出:[null,null,3,-1]
14-
```
14+
<strong>输出:</strong>[null,null,3,-1]
15+
</pre>
1516

16-
**示例 2:**
17+
<p><strong>示例 2:</strong></p>
1718

18-
```
19-
输入:
20-
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
19+
<pre><strong>输入:</strong>
20+
[&quot;CQueue&quot;,&quot;deleteHead&quot;,&quot;appendTail&quot;,&quot;appendTail&quot;,&quot;deleteHead&quot;,&quot;deleteHead&quot;]
2121
[[],[],[5],[2],[],[]]
22-
输出:[null,-1,null,null,5,2]
23-
```
22+
<strong>输出:</strong>[null,-1,null,null,5,2]
23+
</pre>
2424

25-
**提示:**
25+
<p><strong>提示:</strong></p>
2626

27-
- `1 <= values <= 10000`
28-
- `最多会对 appendTail、deleteHead 进行 10000 次调用`
27+
<ul>
28+
<li><code>1 &lt;= values &lt;= 10000</code></li>
29+
<li><code>最多会对&nbsp;appendTail、deleteHead 进行&nbsp;10000&nbsp;次调用</code></li>
30+
</ul>
2931

3032
## 解法
3133

32-
- 两个栈,一个负责**输入**,一个负责**输出**
33-
- 执行输入时,只放入输入栈中;
34-
- 执行输出时,将输入栈的所有元素依次出栈,放入输出栈中;
35-
- 根据栈的特点,处于输入栈**栈底**的元素,在输出栈中便是**栈顶**
36-
- 只有输出栈中没有元素时才进行倒放,而非每一次。
34+
- 两个栈,一个负责**输入**,一个负责**输出**
35+
- 执行输入时,只放入输入栈中;
36+
- 执行输出时,将输入栈的所有元素依次出栈,放入输出栈中;
37+
- 根据栈的特点,处于输入栈**栈底**的元素,在输出栈中便是**栈顶**
38+
- 只有输出栈中没有元素时才进行倒放,而非每一次。
3739

3840
<!-- tabs:start -->
3941

@@ -43,21 +45,17 @@
4345
class CQueue:
4446

4547
def __init__(self):
46-
self.s1 = []
47-
self.s2 = []
48+
self.stk1 = []
49+
self.stk2 = []
4850

4951
def appendTail(self, value: int) -> None:
50-
self.s1.append(value)
52+
self.stk1.append(value)
5153

5254
def deleteHead(self) -> int:
53-
if not self.s2:
54-
self._move()
55-
return -1 if not self.s2 else self.s2.pop()
56-
57-
def _move(self):
58-
while self.s1:
59-
self.s2.append(self.s1.pop())
60-
55+
if not self.stk2:
56+
while self.stk1:
57+
self.stk2.append(self.stk1.pop())
58+
return -1 if not self.stk2 else self.stk2.pop()
6159

6260

6361
# Your CQueue object will be instantiated and called as such:
@@ -70,29 +68,24 @@ class CQueue:
7068

7169
```java
7270
class CQueue {
71+
private Deque<Integer> stk1 = new ArrayDeque<>();
72+
private Deque<Integer> stk2 = new ArrayDeque<>();
7373

74-
private Deque<Integer> s1;
75-
private Deque<Integer> s2;
7674
public CQueue() {
77-
s1 = new ArrayDeque<>();
78-
s2 = new ArrayDeque<>();
79-
}
8075

76+
}
77+
8178
public void appendTail(int value) {
82-
s1.push(value);
79+
stk1.push(value);
8380
}
84-
81+
8582
public int deleteHead() {
86-
if (s2.isEmpty()) {
87-
move();
88-
}
89-
return s2.isEmpty() ? -1 : s2.pop();
90-
}
91-
92-
private void move() {
93-
while (!s1.isEmpty()) {
94-
s2.push(s1.pop());
83+
if (stk2.isEmpty()) {
84+
while (!stk1.isEmpty()) {
85+
stk2.push(stk1.pop());
86+
}
9587
}
88+
return stk2.isEmpty() ? -1 : stk2.pop();
9689
}
9790
}
9891

@@ -108,71 +101,76 @@ class CQueue {
108101

109102
```js
110103
var CQueue = function () {
111-
this.data = [];
112-
this.helper = [];
104+
this.stk1 = [];
105+
this.stk2 = [];
113106
};
107+
114108
/**
115109
* @param {number} value
116110
* @return {void}
117111
*/
118112
CQueue.prototype.appendTail = function (value) {
119-
this.data.push(value);
113+
this.stk1.push(value);
120114
};
115+
121116
/**
122117
* @return {number}
123118
*/
124119
CQueue.prototype.deleteHead = function () {
125-
if (this.data.length) {
126-
while (this.data.length > 1) {
127-
this.helper.push(this.data.pop());
128-
}
129-
let res = this.data.pop();
130-
while (this.helper.length) {
131-
this.data.push(this.helper.pop());
120+
if (!this.stk2.length) {
121+
while (this.stk1.length) {
122+
this.stk2.push(this.stk1.pop());
132123
}
133-
return res;
134-
} else {
135-
return -1;
136124
}
125+
return this.stk2.length ? this.stk2.pop() : -1;
137126
};
127+
128+
/**
129+
* Your CQueue object will be instantiated and called as such:
130+
* var obj = new CQueue()
131+
* obj.appendTail(value)
132+
* var param_2 = obj.deleteHead()
133+
*/
134+
138135
```
139136

140137
### **Go**
141138

142139
```go
143140
type CQueue struct {
144-
Stack1 []int
145-
Stack2 []int
141+
stk1 []int
142+
stk2 []int
146143
}
147144

148-
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
149-
//再从S2弹出
150-
151145
func Constructor() CQueue {
152-
return CQueue{Stack1: []int{}, Stack2: []int{}}
146+
return CQueue{stk1: []int{}, stk2: []int{}}
153147
}
154148

155149
func (this *CQueue) AppendTail(value int) {
156-
this.Stack1 = append(this.Stack1, value)
150+
this.stk1 = append(this.stk1, value)
157151
}
158152

159153
func (this *CQueue) DeleteHead() int {
160-
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
161-
return -1
154+
if len(this.stk2) == 0 {
155+
for len(this.stk1) > 0 {
156+
this.stk2 = append(this.stk2, this.stk1[len(this.stk1)-1])
157+
this.stk1 = this.stk1[0 : len(this.stk1)-1]
158+
}
162159
}
163-
if len(this.Stack2) > 0 {
164-
res := this.Stack2[len(this.Stack2)-1]
165-
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
166-
return res
167-
}
168-
for len(this.Stack1) > 0 {
169-
this.Stack2 = append(this.Stack2, this.Stack1[len(this.Stack1)-1])
170-
this.Stack1 = this.Stack1[0 : len(this.Stack1)-1]
160+
if len(this.stk2) == 0 {
161+
return -1
171162
}
172-
res := this.Stack2[len(this.Stack2)-1]
173-
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
174-
return res
163+
ans := this.stk2[len(this.stk2)-1]
164+
this.stk2 = this.stk2[0 : len(this.stk2)-1]
165+
return ans
175166
}
167+
168+
/**
169+
* Your CQueue object will be instantiated and called as such:
170+
* obj := Constructor();
171+
* obj.AppendTail(value);
172+
* param_2 := obj.DeleteHead();
173+
*/
176174
```
177175

178176
### **C++**
@@ -265,11 +263,11 @@ impl CQueue {
265263
s2: Vec::new(),
266264
}
267265
}
268-
266+
269267
fn append_tail(&mut self, value: i32) {
270268
self.s1.push(value);
271269
}
272-
270+
273271
fn delete_head(&mut self) -> i32 {
274272
match self.s2.pop() {
275273
Some(value) => value,
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
type CQueue struct {
2-
Stack1 []int
3-
Stack2 []int
2+
stk1 []int
3+
stk2 []int
44
}
55

6-
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
7-
//再从S2弹出
8-
96
func Constructor() CQueue {
10-
return CQueue{Stack1: []int{}, Stack2: []int{}}
7+
return CQueue{stk1: []int{}, stk2: []int{}}
118
}
129

1310
func (this *CQueue) AppendTail(value int) {
14-
this.Stack1 = append(this.Stack1, value)
11+
this.stk1 = append(this.stk1, value)
1512
}
1613

1714
func (this *CQueue) DeleteHead() int {
18-
if len(this.Stack1) == 0 && len(this.Stack2) == 0 {
19-
return -1
20-
}
21-
if len(this.Stack2) > 0 {
22-
res := this.Stack2[len(this.Stack2)-1]
23-
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
24-
return res
15+
if len(this.stk2) == 0 {
16+
for len(this.stk1) > 0 {
17+
this.stk2 = append(this.stk2, this.stk1[len(this.stk1)-1])
18+
this.stk1 = this.stk1[0 : len(this.stk1)-1]
19+
}
2520
}
26-
for len(this.Stack1) > 0 {
27-
this.Stack2 = append(this.Stack2, this.Stack1[len(this.Stack1)-1])
28-
this.Stack1 = this.Stack1[0 : len(this.Stack1)-1]
21+
if len(this.stk2) == 0 {
22+
return -1
2923
}
30-
res := this.Stack2[len(this.Stack2)-1]
31-
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1]
32-
return res
33-
}
24+
ans := this.stk2[len(this.stk2)-1]
25+
this.stk2 = this.stk2[0 : len(this.stk2)-1]
26+
return ans
27+
}
28+
29+
/**
30+
* Your CQueue object will be instantiated and called as such:
31+
* obj := Constructor();
32+
* obj.AppendTail(value);
33+
* param_2 := obj.DeleteHead();
34+
*/
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
class CQueue {
2+
private Deque<Integer> stk1 = new ArrayDeque<>();
3+
private Deque<Integer> stk2 = new ArrayDeque<>();
24

3-
private Deque<Integer> s1;
4-
private Deque<Integer> s2;
55
public CQueue() {
6-
s1 = new ArrayDeque<>();
7-
s2 = new ArrayDeque<>();
8-
}
96

7+
}
8+
109
public void appendTail(int value) {
11-
s1.push(value);
10+
stk1.push(value);
1211
}
13-
12+
1413
public int deleteHead() {
15-
if (s2.isEmpty()) {
16-
move();
17-
}
18-
return s2.isEmpty() ? -1 : s2.pop();
19-
}
20-
21-
private void move() {
22-
while (!s1.isEmpty()) {
23-
s2.push(s1.pop());
14+
if (stk2.isEmpty()) {
15+
while (!stk1.isEmpty()) {
16+
stk2.push(stk1.pop());
17+
}
2418
}
19+
return stk2.isEmpty() ? -1 : stk2.pop();
2520
}
2621
}
2722

@@ -30,4 +25,4 @@ private void move() {
3025
* CQueue obj = new CQueue();
3126
* obj.appendTail(value);
3227
* int param_2 = obj.deleteHead();
33-
*/
28+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.