题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
package main var stack1 []int var stack2 []int var len1 = 0 var len2 = 0 func Push(node int) { if len(stack1) <= len1 { stack1 = append(stack1, node) } else { stack1[len1] = node } len1++ } func Pop() int { if len2 <= 0 { transfer() } len2-- return stack2[len2] } func transfer() { for len1 > 0 { if len(stack2) <= len2 { stack2 = append(stack2, stack1[len1-1]) } else { stack2[len2] = stack1[len1-1] } len1-- len2++ } }