力扣232. 用栈实现队列225. 用队列实现栈

232. 用栈实现队列(简单,熟悉栈和队列的用法)

力扣232. 用栈实现队列225. 用队列实现栈_第1张图片

class MyQueue {
public:
  
    stack temp1;
    stack temp2;
    
    MyQueue() { 
    }
    
    void push(int x) {
        while(temp2.empty()!=true)
        {
            int a = temp2.top();
            temp1.push(a);
            temp2.pop();
        }
        temp1.push(x);
    }
    
    int pop() {
        while(temp1.empty()!=true)
        {
            int a = temp1.top();
            temp2.push(a);
            temp1.pop();
        }
        int a = temp2.top();
        temp2.pop();
        return a;
    }
    
    int peek() {
        while(temp1.empty()!=true)
        {
            int a = temp1.top();
            temp2.push(a);
            temp1.pop();
        }
        int a = temp2.top();
        return a;
    }
    
    bool empty() {
        return (temp1.empty() && temp2.empty());
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225. 用队列实现栈(其实可以用一个队列就完成了)

力扣232. 用栈实现队列225. 用队列实现栈_第2张图片

class MyStack {
public:
    queue temp1;
    queue temp2;
    MyStack() {

    }
    
    void push(int x) {
        if(temp2.empty())
        {
            temp1.push(x);
        }
        else
        {
            temp2.push(x);
        }
    }
    
    int pop() {
        if(!temp1.empty())
        {
            int len = temp1.size();
            for(int i= 0; i < len-1; i++)
            {
                int num = temp1.front();
                temp2.push(num);
                temp1.pop();
            }
            int num = temp1.front();
            temp1.pop();
            return num;
        }
        else
        {
            int len = temp2.size();
            for(int i= 0; i < len-1; i++)
            {
                int num = temp2.front();
                temp1.push(num);
                temp2.pop();
            }
            int num = temp2.front();
            temp2.pop();
            return num;
        }
    }
    
    int top() {
        if(!temp1.empty())
        {
            int len = temp1.size();
            for(int i= 0; i < len-1; i++)
            {
                int num = temp1.front();
                temp2.push(num);
                temp1.pop();
            }
            int num = temp1.front();
            temp2.push(num);
            temp1.pop();
            return num;
        }
        else
        {
            int len = temp2.size();
            for(int i= 0; i < len-1; i++)
            {
                int num = temp2.front();
                temp1.push(num);
                temp2.pop();
            }
            int num = temp2.front();
            temp1.push(num);
            temp2.pop();
            return num;
        }
    }
    
    bool empty() {
        return (temp1.empty() && temp2.empty());
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

你可能感兴趣的:(leetcode,算法,java)