<LeetCode OJ> 225. Implement Stack using Queues

来源:互联网 发布:haproxy acl 端口复用 编辑:程序博客网 时间:2024/05/21 09:39

225. Implement Stack using Queues

Total Accepted: 26676 Total Submissions: 88051 Difficulty: Easy

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize
  • and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. 
  • You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.



分析:

感觉没什么特别好的办法,反正两个队列,始终保持某一个为空,始终留一个(这个就是栈顶元素).....
申请两个队列1和2,举例,比如1,2,3,4依次压进队列1,则1应该是栈首,4是栈底
1,进栈:全部压进队列1
2,取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空
3,弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出
4,是否为空?显然来个队列为空就是空栈。

class Stack {public:    // Push element x onto stack.    void push(int x) {        if(!q1.empty())//q1不为空           q1.push(x);         else           q2.push(x);         newtop=x;//新进来的元素就是栈顶元素,pop时一定要更新这个值    }    // Removes the element on top of the stack.    void pop() {        if(!q1.empty())//q1不为空         {               while(q1.size()>1)             {                 int val=q1.front();                 if(q1.size() == 2)                    newtop = val;                 q2.push(val);                 q1.pop();             }             q1.pop();         }        else          {               while(q2.size()>1)             {                 int val=q2.front();                 if(q2.size() == 2)                    newtop = val;                 q1.push(val);                 q2.pop();             }             q2.pop();           }    }    // Get the top element.    int top() {         return newtop;    }    // Return whether the stack is empty.    bool empty() {        return q1.empty() && q2.empty();    }private:    queue<int> q1;      queue<int> q2;     int newtop;};


来自别人家的思路:

只申请一个队列,每次数据压进来的时候,直接就将数据调整为栈的顺序
将x压入队列,接着调整:取队首元素(先进来的元素)重新压倒队尾,操作que.size()-1次
牛逼.......

class Stack {public:    queue<int> que;    // Push element x onto stack.    void push(int x) {        que.push(x);        for(int i=0;i<que.size()-1;++i){            que.push(que.front());            que.pop();        }    }    // Removes the element on top of the stack.    void pop() {        que.pop();    }    // Get the top element.    int top() {        return que.front();    }    // Return whether the stack is empty.    bool empty() {        return que.empty();    }};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50444995

原作者博客:http://blog.csdn.net/ebowtang

1 0
原创粉丝点击