Golang | Leetcode Golang题解之第232题用栈实现队列

avatar
作者
筋斗云
阅读量:4

题目:

题解:

type MyQueue struct {     inStack, outStack []int }  func Constructor() MyQueue {     return MyQueue{} }  func (q *MyQueue) Push(x int) {     q.inStack = append(q.inStack, x) }  func (q *MyQueue) in2out() {     for len(q.inStack) > 0 {         q.outStack = append(q.outStack, q.inStack[len(q.inStack)-1])         q.inStack = q.inStack[:len(q.inStack)-1]     } }  func (q *MyQueue) Pop() int {     if len(q.outStack) == 0 {         q.in2out()     }     x := q.outStack[len(q.outStack)-1]     q.outStack = q.outStack[:len(q.outStack)-1]     return x }  func (q *MyQueue) Peek() int {     if len(q.outStack) == 0 {         q.in2out()     }     return q.outStack[len(q.outStack)-1] }  func (q *MyQueue) Empty() bool {     return len(q.inStack) == 0 && len(q.outStack) == 0 }

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!