leetcode stack 155 225 232

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

發佈了56 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章