leetcode stack 155 225 232

来源:互联网 发布:安装人工智能计算器 编辑:程序博客网 时间:2024/06/08 19:17

easy的栈,155取最小的元素,用两个栈,第二个栈存当前的栈的最小元素,然后弹出的时候根据情况弹出元素:

class MinStack {public:    /** initialize your data structure here. */    MinStack() {            }        void push(int x) {        s1.push_back(x);        if(s2.empty()||x<=getMin())        {            s2.push_back(x);        }    }        void pop() {        if(s1.back()==getMin())        {            s2.pop_back();        }        s1.pop_back();    }        int top() {        return s1.back();       }        int getMin() {        return s2.back();    }    vector<int> s1;    vector<int> s2;};/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */


255用deque,双向队列:

class MyStack {public:    /** Initialize your data structure here. */    MyStack() {            }        /** Push element x onto stack. */    void push(int x) {        s1.push_back(x);    }        /** Removes the element on top of the stack and returns that element. */    int pop() {        int temp=top();        s1.pop_back();        return temp;    }        /** Get the top element. */    int top() {        return s1.back();    }        /** Returns whether the stack is empty. */    bool empty() {        if(s1.empty())        {            return true;        }        return false;    }    deque<int> s1;};/** * 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(); */


232,用两个栈实现一个队列,容易想的就是s2缓存,s1是主要的,push直接在s1后面push就好,但是pop要先倒到s2,再倒回s1,这里倒的时候可以少倒一个,直接pop(这个代码没有体现):

class MyQueue {public:    /** Initialize your data structure here. */    MyQueue() {            }        /** Push element x to the back of queue. */    void push(int x) {        s1.push(x);    }        /** Removes the element from in front of queue and returns that element. */    int pop() {        while(!s1.empty())        {            s2.push(s1.top());            s1.pop();        }        int temp=s2.top();        s2.pop();        while(!s2.empty())        {            s1.push(s2.top());            s2.pop();        }        return temp;    }        /** Get the front element. */    int peek() {        while(!s1.empty())        {            s2.push(s1.top());            s1.pop();        }        int temp=s2.top();        while(!s2.empty())        {            s1.push(s2.top());            s2.pop();        }        return temp;    }        /** Returns whether the queue is empty. */    bool empty() {        if(s1.empty())        {            return true;        }        return false;    }    stack<int> s1;    stack<int> s2;};/** * 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(); */


另一种变种,s2负责弹出头的,s1负责push尾的,一次倒一个,不用倒过去再倒回来,如果push,发现元素都在s2里,就倒回来,否则不用;如果pop,元素都在s1里就倒到s2,否则不用:

class MyQueue {public:    /** Initialize your data structure here. */    MyQueue() {            }        /** Push element x to the back of queue. */    void push(int x) {        if(s2.empty())        s1.push(x);        else        {            while(!s2.empty())            {                s1.push(s2.top());                s2.pop();            }            s1.push(x);        }            }        /** Removes the element from in front of queue and returns that element. */    int pop() {        int temp;        if(s1.empty())        {            temp=s2.top();            s2.pop();        }        else        {            int n=s1.size();            for(int i=0;i<n-1;i++)            {                s2.push(s1.top());                s1.pop();            }            temp=s1.top();            s1.pop();        }        return temp;    }        /** Get the front element. */    int peek() {        int temp;        if(s1.empty())        {            temp=s2.top();        }        else        {            while(!s1.empty()){                s2.push(s1.top());                s1.pop();            }            temp=s2.top();        }        return temp;    }        /** Returns whether the queue is empty. */    bool empty() {        if(s1.empty()&&s2.empty())        {            return true;        }        return false;    }    stack<int> s1;    stack<int> s2;};/** * 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(); */


最简单的变种,只要push就到s1里,pop的时候,s2有元素就从s2里面pop,如果s2中没有元素了,再把s1中的元素都倒到s2里,然后pop,这种倒的次数最少。

class MyQueue {public:    /** Initialize your data structure here. */    MyQueue() {            }        /** Push element x to the back of queue. */    void push(int x) {        s1.push(x);            }        /** Removes the element from in front of queue and returns that element. */    int pop() {        int temp;        if(!s2.empty())        {            temp=s2.top();            s2.pop();        }        else        {            int n=s1.size();            for(int i=0;i<n-1;i++)            {                s2.push(s1.top());                s1.pop();            }            temp=s1.top();            s1.pop();        }        return temp;    }        /** Get the front element. */    int peek() {        int temp;        if(!s2.empty())        {            temp=s2.top();        }        else        {            while(!s1.empty()){                s2.push(s1.top());                s1.pop();            }            temp=s2.top();        }        return temp;    }        /** Returns whether the queue is empty. */    bool empty() {        if(s1.empty()&&s2.empty())        {            return true;        }        return false;    }    stack<int> s1;    stack<int> s2;};/** * 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(); */


可参考:http://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html

0 0
原创粉丝点击