题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
/**
1. s1存储每次push的node
2. pop时,需要拿到s1最底下的node,需要s2辅助
3. s2存储s1pop的值直到s1为空栈,这样顺序就反过来了
4. s2在pop就模拟出了队列
*/
let s1 = []
let s2 = []
function push(node)
{
s1.push(node)
}
function pop()
{
// write code here
if(!s2.length){
while(s1.length){
s2.push(s1.pop())
}
}
return s2.pop()
}
module.exports = {
push : push,
pop : pop
};
查看11道真题和解析
