#40 Implement Queue by Two Stacks

原创 2016年08月29日 04:19:37

题目描述:

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.

Mycode(AC = 37ms):

class Queue {
public:
    stack<int> stack1;
    stack<int> stack2;

    Queue() {
        // do intialization if necessary
    }

    void push(int element) {
        // write your code here
        stack1.push(element);
    }
    
    int pop() {
        // write your code here
        while (!stack1.empty()) {
            stack2.push(stack1.top());
            stack1.pop();
        }
        
        int val = stack2.top();
        stack2.pop();
        while(!stack2.empty()) {
            stack1.push(stack2.top());
            stack2.pop();
        }
        
        return val;
    }

    int top() {
        // write your code here
        while (!stack1.empty()) {
            stack2.push(stack1.top());
            stack1.pop();
        }
        
        int val = stack2.top();
        while(!stack2.empty()) {
            stack1.push(stack2.top());
            stack2.pop();
        }
        
        return val;
    }
};


版权声明:三千微尘里,吾宁爱与憎

implement a queue by using two stacks

For a queue, we need to implement enqueue and dequeue methods. The idea is if we perform enqueue ...
  • beiyetengqing
  • beiyetengqing
  • 2012年08月29日 10:02
  • 704

Leetcode_232_Implement Queue using Stacks

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392363 Implement the foll...
  • pistolove
  • pistolove
  • 2015年09月12日 11:01
  • 1446

Two Stacks In One Array

Write routines to implement two stacks using only one array. Your stack routines should not declare ...
  • dxz935
  • dxz935
  • 2016年10月07日 00:21
  • 1203

Q3.5 Implement a MyQueue class which implements a queue using two stacks.

Q:Implement a MyQueue class which implements a queue using two stacks. A:一个栈sin用于进,一个栈sout用于出。 pus...
  • xinxinxv
  • xinxinxv
  • 2015年01月19日 23:16
  • 321

232. Implement Queue using Stacks [easy] (Python)

题目链接https://leetcode.com/problems/implement-queue-using-stacks/题目原文 Implement the following operat...
  • coder_orz
  • coder_orz
  • 2016年06月04日 23:32
  • 1072

LeetCode232 Implement Queue using Stacks Java 题解

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to...
  • u012249528
  • u012249528
  • 2015年07月09日 12:03
  • 2159

leetcode 232:Implement Queue using Stacks

leetcode 232:Implement Queue using Stacks python java c++
  • xudli
  • xudli
  • 2015年07月07日 04:48
  • 7358

pta Two Stacks In One Array(简单版双向数组)

Write routines to implement two stacks using only one array. Your stack routines should not declare ...
  • vocaloid01
  • vocaloid01
  • 2017年11月12日 20:51
  • 112

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back...
  • sunao2002002
  • sunao2002002
  • 2015年07月07日 19:35
  • 4311

QueueWithTwoStacks

#include using namespace std; #include #include template class CQueue{ public: CQueue(void); ~CQue...
  • u014558484
  • u014558484
  • 2016年08月09日 10:04
  • 213
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:#40 Implement Queue by Two Stacks
举报原因:
原因补充:

(最多只允许输入30个字)