【LeetCode】分类刷题 之 栈和队列

STL

:std::stack S;
S.top() S.empty() S.push(x) S.pop() S.size()
队列:std:queue Q;
Q.empty()、Q.front()、Q.back()、Q.pop()、Q.push()、Q.size()

题225 用队列实现栈

用时:0ms 100% 内存 7MB 100%

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        std::queue<int> temp_queue;
        temp_queue.push(x);
        while(!data_queue.empty()){
            int a = _data.front();
            temp_queue.push(a);
            _data.pop();
        }
        while(!temp_queue.empty()){
            data_queue.push(temp_queue.front());
            temp_queue.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int x = data_queue.front();
        data_queue.pop();
        return x;
    }
    
    /** Get the top element. */
    int top() {
        return data_queue.front();

    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return data_queue.empty();
    }
private:
    std::queue<int> data_queue;
};

/**
 * 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 用栈实现队列

用时:0ms 100% 内存 7.2MB 100%

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        std::stack<int> temp_stack;
        while(!data_stack.empty()){
            temp_stack.push(data_stack.top());
            data_stack.pop();
        }
        temp_stack.push(x);
        while(!temp_stack.empty()){
            data_stack.push(temp_stack.top());
            temp_stack.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int x = data_stack.top();
        data_stack.pop();
        return x;
    }
    
    /** Get the front element. */
    int peek() {
        return data_stack.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return data_stack.empty();
    }
private:
    std::stack<int> data_stack;
};

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

题155 最小栈

用时:40ms 43.19% 内存:15.2MB 100%

class MinStack {
public:
    /** initialize your data structure here. */
    std::stack<int> data;
    std::stack<int> min_data;
    MinStack() {
    }
    
    void push(int x) {
        data.push(x);
        if(min_data.empty()){
            min_data.push(x);
        }else{
            if(x < min_data.top()){
                min_data.push(x);
            }else{
                min_data.push(min_data.top());
            }
        }
    }
    
    void pop() {
        data.pop();
        min_data.pop();
        //最小值栈中最小值的位置和原来栈中位置是一致的
        //所以可以同时弹出
    }
    
    int top() {
        return data.top();
    }
    
    int getMin() {
        return min_data.top();
    }
};

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

题946 验证栈序列

给定 pushed 和 popped 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。
提示:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed 是 popped 的排列。

j j j 指针指向数组 p o p p e d popped popped 可以模拟队列,数 组 p u s h e d pushed pushed 存在栈中。如果栈顶元素和队列元素,则同时弹出。如果栈空,则合法。否则,不合法。

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        std::stack<int> s;
        int n = pushed.size();
        int j = 0;//poped索引
        //将pushed压栈
        for(int i = 0; i < n; i++){
            s.push(pushed[i]);
            while(!s.empty()&&s.top()==popped[j]){
                s.pop();
                j++;
            }
        }
        return s.empty();
    }  
};

你可能感兴趣的:(LeetCode记录)