#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;
    }
};


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

相关文章推荐

LeetCode 之 Implement Queue using Stacks — C++ 实现

Implement Queue using Stacks   Implement the following operations of a queue using stacks. ...

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back...

LeetCode[232] Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back...

LeetCode 232. Implement Queue using Stacks(用栈实现队列)

原题网址:https://leetcode.com/problems/implement-queue-using-stacks/ mplement the following operations...
  • jmspan
  • jmspan
  • 2016年04月05日 11:53
  • 323

[Leetcode] 232. Implement Queue using Stacks 解题报告

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to...

Implement Queue using Stacks

1 题目描述 Implement the following operations of a queue using stacks. push(x) -- Push element x to t...

leetcode Implement Queue using Stacks

继续刷题。编译CM12.1刷机怎么着都有问题,烦人的把之前下载的源码直接删掉,重新下载,在下载源码期间继续刷题,希望这个easy不要把我虐成狗。 其实主要问题是自己米有java基础知识,幸亏会点C,可...

[LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back o...

Leetcode232. Implement Queue using Stacks[栈实现队列]

Leetcode232. Implement Queue using Stacks[栈实现队列]

<LeetCode OJ> 232. Implement Queue using Stacks

232. Implement Queue using Stacks My Submissions Question Total Accepted: 28236 Total Submission...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:#40 Implement Queue by Two Stacks
举报原因:
原因补充:

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