Skip to content

【算法系列 - 剑指Offer】实现队列 #18

@AwesomeDevin

Description

@AwesomeDevin

题目1:

实现1个队列

JS实现

const stack1 = []
function push(node)
{
    stack1.push(node)
    // write code here
}
function pop()
{
    return stack1.shift()
    // write code here
}

题目2:

用两个栈实现1个队列

JS实现

var stack1 = []
var stack2 = []
function push(node)
{
    stack1.push(node)
    // write code here
}
function pop()
{
    while(stack1.length>1)
    {
         stack2.push(stack1.pop())
    }
    var res = stack1[0]
    stack1 = stack2.reverse()
    stack2 = []
    return res
    // write code here
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions