Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac2aa71

Browse files
[fix][golang] implement-stack-using-queues (labuladong#1601)
* [fix][golang] implement-stack-using-queues * tab to space
1 parent 291e8cd commit ac2aa71

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

‎多语言解法代码/solution_code.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30246,27 +30246,30 @@ public:
3024630246
```
3024730247

3024830248
```go
30249-
// by chatGPT (go)
30249+
// by mario-huang (go)
30250+
package ImplementStackusingQueues
30251+
3025030252
type MyStack struct {
30251-
q []int
30252-
topElem int
30253+
q []int
30254+
topElem int
3025330255
}
3025430256

30255-
/** Initialize your data structure here. */
3025630257
func Constructor() MyStack {
30257-
return MyStack{
30258-
q: make([]int, 0),
30259-
}
30258+
return MyStack{q: []int{}, topElem: 0}
3026030259
}
3026130260

30262-
/** Push element x onto stack. */
30261+
/**
30262+
* 添加元素到栈顶
30263+
*/
3026330264
func (this *MyStack) Push(x int) {
3026430265
// x 是队列的队尾,是栈的栈顶
3026530266
this.q = append(this.q, x)
3026630267
this.topElem = x
3026730268
}
3026830269

30269-
/** Removes the element on top of the stack and returns that element. */
30270+
/**
30271+
* 删除栈顶的元素并返回
30272+
*/
3027030273
func (this *MyStack) Pop() int {
3027130274
size := len(this.q)
3027230275
// 留下队尾 2 个元素
@@ -30280,15 +30283,21 @@ func (this *MyStack) Pop() int {
3028030283
this.q = append(this.q, this.q[0])
3028130284
this.q = this.q[1:]
3028230285
// 删除之前的队尾元素
30283-
return this.q[0]
30286+
val := this.q[0]
30287+
this.q = this.q[1:]
30288+
return val
3028430289
}
3028530290

30286-
/** Get the top element. */
30291+
/**
30292+
* 返回栈顶元素
30293+
*/
3028730294
func (this *MyStack) Top() int {
3028830295
return this.topElem
3028930296
}
3029030297

30291-
/** Returns whether the stack is empty. */
30298+
/**
30299+
* 判断栈是否为空
30300+
*/
3029230301
func (this *MyStack) Empty() bool {
3029330302
return len(this.q) == 0
3029430303
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /