美文网首页
2018-10-22 Implement Queue by Tw

2018-10-22 Implement Queue by Tw

作者: WenshengL | 来源:发表于2018-10-23 07:54 被阅读0次
  1. Implement Queue by Two Stacks
    LC: 232
    Similar problem: Implement Stack by Two Queues [E]

As the title described, you should only use two stacks to implement a queue's actions.

The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

Example
push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2
Challenge
implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.

相关文章

网友评论

      本文标题:2018-10-22 Implement Queue by Tw

      本文链接:https://www.haomeiwen.com/subject/luhozftx.html