File tree 1 file changed +21
-12
lines changed
1 file changed +21
-12
lines changed Original file line number Diff line number Diff line change @@ -30246,27 +30246,30 @@ public:
30246
30246
```
30247
30247
30248
30248
```go
30249
- // by chatGPT (go)
30249
+ // by mario-huang (go)
30250
+ package ImplementStackusingQueues
30251
+
30250
30252
type MyStack struct {
30251
- q []int
30252
- topElem int
30253
+ q []int
30254
+ topElem int
30253
30255
}
30254
30256
30255
- /** Initialize your data structure here. */
30256
30257
func Constructor() MyStack {
30257
- return MyStack{
30258
- q: make([]int, 0),
30259
- }
30258
+ return MyStack{q: []int{}, topElem: 0}
30260
30259
}
30261
30260
30262
- /** Push element x onto stack. */
30261
+ /**
30262
+ * 添加元素到栈顶
30263
+ */
30263
30264
func (this *MyStack) Push(x int) {
30264
30265
// x 是队列的队尾,是栈的栈顶
30265
30266
this.q = append(this.q, x)
30266
30267
this.topElem = x
30267
30268
}
30268
30269
30269
- /** Removes the element on top of the stack and returns that element. */
30270
+ /**
30271
+ * 删除栈顶的元素并返回
30272
+ */
30270
30273
func (this *MyStack) Pop() int {
30271
30274
size := len(this.q)
30272
30275
// 留下队尾 2 个元素
@@ -30280,15 +30283,21 @@ func (this *MyStack) Pop() int {
30280
30283
this.q = append(this.q, this.q[0])
30281
30284
this.q = this.q[1:]
30282
30285
// 删除之前的队尾元素
30283
- return this.q[0]
30286
+ val := this.q[0]
30287
+ this.q = this.q[1:]
30288
+ return val
30284
30289
}
30285
30290
30286
- /** Get the top element. */
30291
+ /**
30292
+ * 返回栈顶元素
30293
+ */
30287
30294
func (this *MyStack) Top() int {
30288
30295
return this.topElem
30289
30296
}
30290
30297
30291
- /** Returns whether the stack is empty. */
30298
+ /**
30299
+ * 判断栈是否为空
30300
+ */
30292
30301
func (this *MyStack) Empty() bool {
30293
30302
return len(this.q) == 0
30294
30303
}
You can’t perform that action at this time.
0 commit comments