@@ -82,7 +82,7 @@ push之前是这样的:
8282
8383## 代码
8484
85- * 语言支持:JS, Python, Java
85+ * 语言支持:JS, Python, Java, Go
8686
8787Javascript Code:
8888
@@ -251,7 +251,55 @@ class MyQueue {
251251 * int param_3 = obj.peek();
252252 * boolean param_4 = obj.empty();
253253 */
254- ````
254+ ```
255+
256+ Go Code:
257+
258+ ``` go
259+ type MyQueue struct {
260+ StackPush []int
261+ StackPop []int
262+ }
263+
264+ /* * Initialize your data structure here. */
265+ func Constructor () MyQueue {
266+ return MyQueue{}
267+ }
268+
269+ /* * Push element x to the back of queue. */
270+ func (this *MyQueue ) Push (x int ) {
271+ this.StackPush = append (this.StackPush , x)
272+ }
273+
274+ /* * Removes the element from in front of queue and returns that element. */
275+ func (this *MyQueue ) Pop () int {
276+ this.Transfer ()
277+ var x int
278+ x, this.StackPop = this.StackPop [0 ], this.StackPop [1 :]
279+ return x
280+ }
281+
282+ /* * Get the front element. */
283+ func (this *MyQueue ) Peek () int {
284+ this.Transfer ()
285+ return this.StackPop [0 ]
286+ }
287+
288+ /* * Returns whether the queue is empty. */
289+ func (this *MyQueue ) Empty () bool {
290+ return len (this.StackPop ) == 0 && len (this.StackPush ) == 0
291+ }
292+
293+ // StackPush 不为空的时候, 转移到 StackPoP 中
294+ func (this *MyQueue ) Transfer () {
295+ var x int
296+ for len (this.StackPush )>0 {
297+ x, this.StackPush = this.StackPush [0 ], this.StackPush [1 :] // pop
298+ this.StackPop = append (this.StackPop , x) // push
299+ }
300+ }
301+ ```
302+
255303
256304** 复杂度分析**
257305- 时间复杂度:$O(1)$
0 commit comments