2018-10-22 Implement Queue by Two Stacks [M]

  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.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,863評(píng)論 0 10
  • 2018-08-06 國(guó)學(xué)誦讀感恩日志1篇 2018年8月6日 星期一天氣晴 今天開始系統(tǒng)讀經(jīng)第3周,第2天 讀經(jīng)...
    琳艷格格閱讀 259評(píng)論 0 0
  • 名就 流言 禮贈(zèng)
    挽筆閱讀 301評(píng)論 0 0
  • 本來也沒準(zhǔn)備干啥,一大早被別人愚了,點(diǎn)開了一個(gè)偽裝紅包,順勢(shì)把微信轉(zhuǎn)發(fā)公司娛樂群,頓時(shí)辦公室響起一片笑聲和笑罵聲。...
    林蔭兒閱讀 451評(píng)論 0 0

友情鏈接更多精彩內(nèi)容