Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][golang] implement-stack-using-queues #1601

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -30241,27 +30241,30 @@ public:
```

```go
// by chatGPT (go)
// by mario-huang (go)
package ImplementStackusingQueues

type MyStack struct {
q []int
topElem int
q []int
topElem int
}

/** Initialize your data structure here. */
func Constructor() MyStack {
return MyStack{
q: make([]int, 0),
}
return MyStack{q: []int{}, topElem: 0}
}

/** Push element x onto stack. */
/**
* 添加元素到栈顶
*/
func (this *MyStack) Push(x int) {
// x 是队列的队尾,是栈的栈顶
this.q = append(this.q, x)
this.topElem = x
}

/** Removes the element on top of the stack and returns that element. */
/**
* 删除栈顶的元素并返回
*/
func (this *MyStack) Pop() int {
size := len(this.q)
// 留下队尾 2 个元素
Expand All @@ -30275,15 +30278,21 @@ func (this *MyStack) Pop() int {
this.q = append(this.q, this.q[0])
this.q = this.q[1:]
// 删除之前的队尾元素
return this.q[0]
val := this.q[0]
this.q = this.q[1:]
return val
}

/** Get the top element. */
/**
* 返回栈顶元素
*/
func (this *MyStack) Top() int {
return this.topElem
}

/** Returns whether the stack is empty. */
/**
* 判断栈是否为空
*/
func (this *MyStack) Empty() bool {
return len(this.q) == 0
}
Expand Down