面试题:两个栈模拟队列&&两个队列模拟栈

两个栈模拟队列(leetcode 面试题09)

解决方案:定义栈s1和s2,入队放s1,出队的话优先取s2,s2为空则将s1的全部转到s2再取。

相当于s1放当前后缀,s2放当前前缀。

class CQueue {
public:

    stack<int>s1, s2;
    CQueue() {
    }
    
    void appendTail(int value) {
        s1.push(value);
    }
    
    int deleteHead() {
        if(s1.empty() && s2.empty())  return -1;
        if(s2.empty())
        {
            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }
        int tmp = s2.top();
        s2.pop();
        return tmp;
    }
};

两个队列模拟栈(leetcode225)

解决方案:入栈:将元素进队列A。出栈:判断队列A中元素的个数是否为1,如果等于1,则出队列;如果A中元素多于1,则将队列A中的元素以此出队并放入队列B,直到队列A中的元素留下一个,然后队列A出队列;否则,A为空,同理将B转移到A直至剩一个。

另一种解法:用一个队列,更好,入栈直接push,出栈将前n-1出队再push,相当于做个循环。

#include<bits/stdc++.h>
using namespace std;

class MyStack {
private:
    queue<int>q1, q2;
public:
    /** Initialize your data structure here. */

    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        q1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int ret;
        if(q1.empty() && q2.empty())  return -1;
        int sz = q1.size();
        if(sz == 1)
        {
            ret = q1.front();
            q1.pop();
        }
        else if(sz > 1)
        {
            while(q1.size() > 1)
            {
                q2.push(q1.front());
                q1.pop();
            }
            ret = q1.front();
            q1.pop();
        }
        else
        {
            while(q2.size() > 1)
            {
                q1.push(q2.front());
                q2.pop();
            }
            ret = q2.front();
            q2.pop();
        }
        return ret;
    }

    /** Get the top element. */
    int top() {
        int ret;
        if(q1.empty() && q2.empty())  return -1;
        int sz = q1.size();
        if(sz == 1)
        {
            ret = q1.front();
            //q1.pop();
        }
        else if(sz > 1)
        {
            while(q1.size() > 1)
            {
                q2.push(q1.front());
                q1.pop();
            }
            ret = q1.front();
            //q1.pop();
        }
        else
        {
            while(q2.size() > 1)
            {
                q1.push(q2.front());
                q2.pop();
            }
            ret = q2.front();
            //q2.pop();
            q1.push(q2.front());  // q2剩下的一个要转移完,不然顺序会不对
            q2.pop();
        }
        return ret;
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return q1.empty() && q2.empty();
    }
};

int main()
{
    MyStack* obj = new MyStack();
    obj->push(1);
    obj->push(2);
    obj->push(3);
    cout << obj->top() << endl;
    cout <<obj->pop() << endl;
    cout << obj->top() << endl;
    cout <<obj->pop() << endl;
    cout << obj->top() << endl;
}

 

 

参考链接:https://blog.csdn.net/violet_echo_0908/article/details/50986516

posted @ 2020-03-07 20:39  Rogn  阅读(745)  评论(0编辑  收藏  举报