2
2
3
3
## 题目描述
4
4
5
- 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 ` appendTail ` 和 ` deleteHead ` ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,` deleteHead ` 操作返回 -1 )
5
+ < p > 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 < code > appendTail</ code > 和 < code > deleteHead</ code > ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,< code > deleteHead</ code > & nbsp ; 操作返回 -1 )</ p >
6
6
7
- ** 示例 1: **
7
+ < p > & nbsp ; </ p >
8
8
9
- ```
10
- 输入:
11
- ["CQueue","appendTail","deleteHead","deleteHead"]
9
+ <p ><strong >示例 1:</strong ></p >
10
+
11
+ <pre ><strong >输入:</strong >
12
+ [" ; CQueue" ; ," ; appendTail" ; ," ; deleteHead" ; ," ; deleteHead" ; ]
12
13
[[],[3],[],[]]
13
- 输出:[null,null,3,-1]
14
- ```
14
+ < strong > 输出:</ strong > [null,null,3,-1]
15
+ </ pre >
15
16
16
- ** 示例 2:**
17
+ < p >< strong > 示例 2:</ strong ></ p >
17
18
18
- ```
19
- 输入:
20
- ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
19
+ <pre ><strong >输入:</strong >
20
+ [" ; CQueue" ; ," ; deleteHead" ; ," ; appendTail" ; ," ; appendTail" ; ," ; deleteHead" ; ," ; deleteHead" ; ]
21
21
[[],[],[5],[2],[],[]]
22
- 输出:[null,-1,null,null,5,2]
23
- ```
22
+ < strong > 输出:</ strong > [null,-1,null,null,5,2]
23
+ </ pre >
24
24
25
- ** 提示:**
25
+ < p >< strong > 提示:</ strong ></ p >
26
26
27
- - ` 1 <= values <= 10000 `
28
- - ` 最多会对 appendTail、deleteHead 进行 10000 次调用 `
27
+ <ul >
28
+ <li><code>1 <= values <= 10000</code></li>
29
+ <li><code>最多会对 appendTail、deleteHead 进行 10000 次调用</code></li>
30
+ </ul >
29
31
30
32
## 解法
31
33
32
- - 两个栈,一个负责** 输入** ,一个负责** 输出** ;
33
- - 执行输入时,只放入输入栈中;
34
- - 执行输出时,将输入栈的所有元素依次出栈,放入输出栈中;
35
- - 根据栈的特点,处于输入栈** 栈底** 的元素,在输出栈中便是** 栈顶** ;
36
- - 只有输出栈中没有元素时才进行倒放,而非每一次。
34
+ - 两个栈,一个负责** 输入** ,一个负责** 输出** ;
35
+ - 执行输入时,只放入输入栈中;
36
+ - 执行输出时,将输入栈的所有元素依次出栈,放入输出栈中;
37
+ - 根据栈的特点,处于输入栈** 栈底** 的元素,在输出栈中便是** 栈顶** ;
38
+ - 只有输出栈中没有元素时才进行倒放,而非每一次。
37
39
38
40
<!-- tabs:start -->
39
41
43
45
class CQueue :
44
46
45
47
def __init__ (self ):
46
- self .s1 = []
47
- self .s2 = []
48
+ self .stk1 = []
49
+ self .stk2 = []
48
50
49
51
def appendTail (self , value : int ) -> None :
50
- self .s1 .append(value)
52
+ self .stk1 .append(value)
51
53
52
54
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()
61
59
62
60
63
61
# Your CQueue object will be instantiated and called as such:
@@ -70,29 +68,24 @@ class CQueue:
70
68
71
69
``` java
72
70
class CQueue {
71
+ private Deque<Integer > stk1 = new ArrayDeque<> ();
72
+ private Deque<Integer > stk2 = new ArrayDeque<> ();
73
73
74
- private Deque<Integer > s1;
75
- private Deque<Integer > s2;
76
74
public CQueue () {
77
- s1 = new ArrayDeque<> ();
78
- s2 = new ArrayDeque<> ();
79
- }
80
75
76
+ }
77
+
81
78
public void appendTail (int value ) {
82
- s1 . push(value);
79
+ stk1 . push(value);
83
80
}
84
-
81
+
85
82
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
+ }
95
87
}
88
+ return stk2. isEmpty() ? - 1 : stk2. pop();
96
89
}
97
90
}
98
91
@@ -108,71 +101,76 @@ class CQueue {
108
101
109
102
``` js
110
103
var CQueue = function () {
111
- this .data = [];
112
- this .helper = [];
104
+ this .stk1 = [];
105
+ this .stk2 = [];
113
106
};
107
+
114
108
/**
115
109
* @param {number} value
116
110
* @return {void}
117
111
*/
118
112
CQueue .prototype .appendTail = function (value ) {
119
- this .data .push (value);
113
+ this .stk1 .push (value);
120
114
};
115
+
121
116
/**
122
117
* @return {number}
123
118
*/
124
119
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 ());
132
123
}
133
- return res;
134
- } else {
135
- return - 1 ;
136
124
}
125
+ return this .stk2 .length ? this .stk2 .pop () : - 1 ;
137
126
};
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
+
138
135
```
139
136
140
137
### ** Go**
141
138
142
139
``` go
143
140
type CQueue struct {
144
- Stack1 []int
145
- Stack2 []int
141
+ stk1 []int
142
+ stk2 []int
146
143
}
147
144
148
- // 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2
149
- // 再从S2弹出
150
-
151
145
func Constructor () CQueue {
152
- return CQueue{Stack1 : []int {}, Stack2 : []int {}}
146
+ return CQueue{stk1 : []int {}, stk2 : []int {}}
153
147
}
154
148
155
149
func (this *CQueue ) AppendTail (value int ) {
156
- this.Stack1 = append (this.Stack1 , value)
150
+ this.stk1 = append (this.stk1 , value)
157
151
}
158
152
159
153
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
+ }
162
159
}
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
171
162
}
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
175
166
}
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
+ */
176
174
```
177
175
178
176
### ** C++**
@@ -265,11 +263,11 @@ impl CQueue {
265
263
s2 : Vec :: new (),
266
264
}
267
265
}
268
-
266
+
269
267
fn append_tail (& mut self , value : i32 ) {
270
268
self . s1. push (value );
271
269
}
272
-
270
+
273
271
fn delete_head (& mut self ) -> i32 {
274
272
match self . s2. pop () {
275
273
Some (value ) => value ,
0 commit comments