2019-12-11 刷题-3(栈)

2019-12-11  本文已影响0人  nowherespyfly

155-最小栈

class MinStack {
public:
    stack<int> s;
    stack<int> m;
    /** initialize your data structure here. */
    MinStack() {
    }
    
    void push(int x) {
        s.push(x);
        if (m.empty())
            m.push(x);
        else{
            int cur_min = m.top();
            if (x < cur_min)
                m.push(x);
            else
                m.push(cur_min);
        }
    }
    
    void pop() {
        s.pop();
        m.pop();
    }
    
    int top() {
        return s.top();
    }
    
    int getMin() {
        return m.top();
    }
};
class MinStack {
public:
    stack<int> s;
    int cur_min;
    /** initialize your data structure here. */
    MinStack() {
    }
    
    void push(int x) {
        if (s.empty()){
            s.push(x);   //第一个元素要压栈两次
            s.push(x);
            cur_min = x;
        }
        else{
            if (x <= cur_min){   //等号很重要
                s.push(cur_min);
                s.push(x);
                cur_min = x;
            }
            else{
                s.push(x);
            }
        }
    }
    
    void pop() {
        if (s.top() == cur_min){
            s.pop();
            cur_min = s.top();
            s.pop();
        }
        else{
            s.pop();
        }
    }
    
    int top() {
        return s.top();
    }
    
    int getMin() {
        return cur_min;
    }
};

225-用队列实现栈

class MyStack {
public:
    /** Initialize your data structure here. */
    queue <int> q1;
    int top_ele;
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        if (q1.empty())
            q1.push(x);
        else {
            int q_len = q1.size();
            q1.push(x);
            while (q_len--) {
                int tmp = q1.front();
                q1.pop();
                q1.push(tmp);
            }
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int rm_ele = q1.front();
        q1.pop();
        return rm_ele;
    }

    /** Get the top element. */
    int top() {
        return q1.front();
    }

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

232-用栈实现队列
双栈法,一个主栈,一个辅助栈。push的时候只push到主栈中。

class MyQueue {
public:
    /** Initialize your data structure here. */
    stack <int> s1;  // store elements with stack order
    stack <int> s2;  // store elements with queue order
    int top_all, top_s1;
    MyQueue() {
    }

    /** Push element x to the back of queue. */
    void push(int x) {
        if (s1.empty() && s2.empty()) {
            top_all = x;
        }
        if (s1.empty() && !s2.empty()) {
            top_s1 = x;   // s1栈底元素,用于s2空时更新队头元素
        }
        s1.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int rm_ele;
        if (!s2.empty()) {
            // pop element from s2
            rm_ele = s2.top();
            s2.pop();
            // update top_all
            if (s2.empty())
                top_all = top_s1;
            else
                top_all = s2.top();
            return rm_ele;
        }
        // transmit all elements from s1 to s2
        while (!s1.empty()) {
            int tmp = s1.top();
            s2.push(tmp);
            s1.pop();
        }
        rm_ele = s2.top();
        s2.pop();
        if (!s2.empty())
            // update top_all
            top_all = s2.top();
        return rm_ele;
    }

    /** Get the front element. */
    int peek() {
        return top_all;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty() && s2.empty();
    }
};

不过,这两种解法提交后运行时间都是0ms,反而是如果不维护top元素,每次取队头元素都要倒一遍数据的做法用时比较多,可能测试样例中比较多peek操作。而维护top元素的代价很小,所以还是很值得的。

上一篇 下一篇

猜你喜欢

热点阅读