LeetCode DAY9(232. Implement Queue using Stacks&225. Implement Stack using Queues)

Preface

This is a new day to start my stack and queue journey.
Learn something new and keep reviewing what I learnt before.

1. Implement Queue using Stacks

LeetCode Link: 232. Implement Queue using Stacks
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

void push(int x) Pushes element x to the back of the queue.
int pop() Removes the element from the front of the queue and returns it.
int peek() Returns the element at the front of the queue.
boolean empty() Returns true if the queue is empty, false otherwise.

Analysis and Solution

Double Stack

LeetCode C++ as followings Double Stack

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    /** Initialize your data structure here. */
    MyQueue() {

    }
    /** Push element x to the back of queue. */
    void push(int x) {
        stIn.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        // Import all stIn data only when stOut is empty
        if (stOut.empty()) {
            // Import all stIn data
            while(!stIn.empty()) {//stIn is not empty
                stOut.push(stIn.top());//Import all stIn data
                stIn.pop();//
            }
        }
        int result = stOut.top();//record value to result
        stOut.pop();//pop
        return result;
    }

    /** Get the front element. */
    int peek() {
        int res = this->pop(); // directly apply pop function 
        stOut.push(res); // add the value because pop function poped it
        return res;//show the value
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return stIn.empty() && stOut.empty();//make a judgement
    }
};

/**
 * 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();
 */

2. Implement Stack using Queues

LeetCode Link: 225. Implement Stack using Queues
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

void push(int x) Pushes element x to the top of the stack.
int pop() Removes the element on the top of the stack and returns it.
int top() Returns the element on the top of the stack.
boolean empty() Returns true if the stack is empty, false otherwise.

Analysis and Solution

Double Queue

LeetCode C++ as followings Double Queue

class MyStack {
public:
    queue<int> que1;
    queue<int> que2; // assist queue; for backup
    /** Initialize your data structure here. */
    MyStack() {

    }

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

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que1.size();
        size--;
        while (size--) { // import que1 to que2;save the last element
            que2.push(que1.front());//
            que1.pop();
        }

        int result = que1.front(); // last element is the value that need to be return
        que1.pop();
        que1 = que2; // assign que2 to que1
        while (!que2.empty()) { // empty que2
            que2.pop();
        }
        return result;
    }

    /** Get the top element. */
    int top() {
        return que1.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que1.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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值