您的位置:首页 > 产品设计 > UI/UE

<LeetCode OJ>Implement Stack using Queues【225】

2016-01-01 14:52 567 查看


225. Implement Stack using Queues

My Submissions

Question

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 back
,
peek/pop from front
,
size
,
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.
Credits:

Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

Subscribe to see which companies asked this question

Hide Tags
Stack Design

Hide Similar Problems
(E) Implement Queue using Stacks

//思路首先:感觉没什么特别好的办法,反正两个队列,始终保持某一个为空,始终留一个(这个就是栈顶元素).....
//申请两个队列1和2,举例,比如1,2,3,4压进队列1,则1是栈首,4才是低
//进栈:全部压进队列1
//取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空
//弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出
//是否为空?显然来个队列为空就是空栈。
class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        if(!q1.empty())//q1不为空
           q1.push(x); 
        else
           q2.push(x); 
    }

    // Removes the element on top of the stack.
    void pop() {
        if(!q1.empty())//q1不为空
         {  
             while(q1.size()>1)
             {
                 int val=q1.front();
                 q2.push(val);
                 q1.pop();
             }
             q1.pop();
         }
        else
          { 
              while(q2.size()>1)
             {
                 int val=q2.front();
                 q1.push(val);
                 q2.pop();
             }
             q2.pop(); 
          }
    }

    // Get the top element.
    int top() {
         if(!q1.empty())//q1不为空
         {  
             while(q1.size()>1)
             {
                 int val=q1.front();
                 q2.push(val);
                 q1.pop();
             }
            int key= q1.front();
            q1.pop();
            q2.push(key);
            return key;
         }
        else
          { 
              while(q2.size()>1)
             {
                 int val=q2.front();
                 q1.push(val);
                 q2.pop();
             }
             int key=q2.front(); 
             q2.pop();
             q1.push(key);
             return key;
          }
    }

    // Return whether the stack is empty.
    bool empty() {
        return q1.empty() && q2.empty();
    }
private:
    queue<int> q1;  
    queue<int> q2; 
};


对上面的程序简单优化了一下:

//思路首先:感觉没什么特别好的办法,反正两个队列,始终就是留一个.....
//申请两个队列1和2,举例,比如1,2,3,4压进队列1,则1是栈首,4才是低
//进栈:全部压进队列1
//取栈顶元素:将队列1的元素转给队列2,队列1只剩1个即可,此元素就是栈首,最后再次压入队列2,使队列1为空,也就是说始终保持队列1或者2为空
//弹出栈顶元素:如同取栈顶元素一样,留下的那一个弹出
//是否为空?显然来个队列为空就是空栈。
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;
};


来自别人家的思路:

//别人家的思路:每次数据压进来的时候,直接就将数据调整为栈的顺序
//即队首重新压倒队尾,操作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();
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: