题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
package main
var stack1 []int
var stack2 []int
// 栈 后进先出
// 队列 先进先出
func Push(node int) {
stack1 = append(stack1, node)
}
func Pop() int {
if len(stack2) == 0 {
for len(stack1) > 0 {
top := stack1[len(stack1)-1]
stack1 = stack1[:len(stack1)-1]
stack2 = append(stack2, top)
}
}
top := stack2[len(stack2)-1]
stack2 = stack2[:len(stack2)-1]
return top
}
查看12道真题和解析