【队列 & 栈】(三) 栈:先入后出的数据结构

本文详细介绍了栈这一后入先出的数据结构,并通过四个典型问题:最小栈、有效括号、每日温度和逆波兰表达式求值,阐述了栈在解决实际问题中的应用。每个问题都包含了题目要求和解决过程,以及不同的解决方案和性能分析。
摘要由CSDN通过智能技术生成

目录

一、后入先出的数据结构

二、栈 - 用法

三、最小栈

3.1 题目要求

3.2 解决过程

四、有效的括号

4.1 题目要求

4.2 解决过程

五、每日温度

5.1 题目要求

5.2 解决过程

六、逆波兰表达式求值

6.1 题目要求

6.2 解决过程



一、后入先出的数据结构

                                                       

// C++ implementation
#include <iostream>

class MyStack {
    private:
        vector<int> data;               // store elements
    public:
        /** Insert an element into the stack. */
        void push(int x) {
            data.push_back(x);
        }
        /** Checks whether the queue is empty or not. */
        bool isEmpty() {
            return data.empty();
        }
        /** Get the top item from the queue. */
        int top() {
            return data.back();
        }
        /** Delete an element from the queue. Return true if the operation is successful. */
        bool pop() {
            if (isEmpty()) {
                return false;
            }
            data.pop_back();
            return true;
        }
};

int main() {
    MyStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    for (int i = 0; i < 4; ++i) {
        if (!s.isEmpty()) {
            cout << s.top() << endl;
        }
        cout << (s.pop() ? "true" : "false") << endl;
    }
}

参考文献:https://leetcode-cn.com/leetbook/read/queue-stack/ghqur/


二、栈 - 用法

// C++ implementation
#include <iostream>

int main() {
    // 1. Initialize a stack.
    stack<int> s;
    // 2. Push new element.
    s.push(5);
    s.push(13);
    s.push(8);
    s.push(6);
    // 3. Check if stack is empty.
    if (s.empty()) {
        cout << "Stack is empty!" << endl;
        return 0;
    }
    // 4. Pop an element.
    s.pop();
    // 5. Get the top element.
    cout << "The top element is: " << s.top() << endl;
    // 6. Get the size of the stack.
    cout << "The size is: " << s.size() << endl;
}

参考文献:https://leetcode-cn.com/leetbook/read/queue-stack/gxtls/


三、最小栈

3.1 题目要求

3.2 解决过程

个人实现

法一:基于内置列表类型。getMin() 操作复杂度太高使整体效率不足,需要改进其实现方式。

2020/08/30 - 15.99% (716ms)

class MinStack:
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.minstack = []

    def push(self, x: int) -> None:
        self.minstack.append(x)    # 复杂度 O(1)

    def pop(self) -> None:
        self.minstack.pop()        # 复杂度 O(1)

    def top(self) -> int:
        return self.minstack[-1]   # 复杂度 O(1)

    def getMin(self) -> int:
        return min(self.minstack)  # 复杂度 O(n)

官方实现与说明

                    

                                                    

                    

class MinStack:
    def __init__(self):
        self.stack = []  # 主栈
        self.min_stack = [math.inf]  # 辅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值