LeetCode刷题--点滴记录010

10. 剑指 Offer 30. 包含 min 函数的栈

要求

定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。

解题

C++版本
#include <iostream>
using namespace std;
#include <stack>

class MinStack {
public:
    stack<int> A;
    stack<int> B;
    MinStack() {}
    void push(int x) {
        A.push(x);   // 将x添加到栈A中
        if (B.empty() || B.top() >= x)
            B.push(x); // 将x添加到栈B中
    }
    void pop() {
        if (A.top() == B.top()) // 判断栈A栈顶元素是否与栈B栈顶元素相等
            B.pop();// 栈B弹出栈顶元素
        A.pop();  // 弹出栈A栈顶元素
    }
    int top() {
        return A.top(); // 返回栈A栈顶元素
    }
    int min() {
        return B.top(); // 返回栈B栈顶元素
    }
};

void test01()
{
    MinStack m;
    m.push(0);
    m.push(3);
    m.push(-3);
    cout << m.min() << endl;
    m.pop();
    cout << m.top() << endl;
    cout << m.min() << endl;
}

int main()
{
    test01();

    system("pause");
    return 0;
}

在这里插入图片描述

Python版本
class MinStack:
    def __init__(self):
        self.A = []
        self.B = []

    def push(self, x):
        self.A.append(x)   # 将x添加到栈A中
        if not self.B or self.B[-1] >= x:
            self.B.append(x)   # 将x添加到栈B中

    def pop(self):
        if self.A.pop() == self.B[-1]:  # 判断栈A弹出元素是否与栈B栈顶元素相等
            self.B.pop()   # 栈B弹出栈顶元素

    def top(self):
        return self.A[-1]  # 返回栈A栈顶元素

    def min(self):
        return self.B[-1]  # 返回栈B栈顶元素

def test01():
    minstack = MinStack()
    minstack.push(0)
    minstack.push(3)
    minstack.push(-3)
    print(minstack.min())
    minstack.pop()
    print(minstack.top())
    print(minstack.min())
            
if __name__=="__main__":
    test01()

在这里插入图片描述

Java版本
package com.hailei_01;

import java.util.Stack;

public class minStack {
    public static void main(String[] args) {
        MinStack C = new MinStack();
        C.push(0);
        C.push(3);
        C.push(-3);
        System.out.println(C.min());
        C.pop();
        System.out.println(C.top());
        System.out.println(C.min());
    }

    public static class MinStack {
        static Stack<Integer> A;
        static Stack<Integer> B;
        public MinStack() {
            A = new Stack<>();
            B = new Stack<>();
        }
        public static void push(int x) {
            A.add(x); // 将x添加到栈A中
            if(B.empty() || B.peek() >= x)
                B.add(x); // 将x添加到栈B中
        }
        public static void pop() {
            if(A.pop().equals(B.peek())) // 判断栈A弹出元素是否与栈B栈顶元素相等
                B.pop(); // 栈B弹出栈顶元素
        }
        public static int top() {
            return A.peek(); // 返回栈A栈顶元素
        }
        public static int min() {
            return B.peek(); // 返回栈B栈顶元素
        }
    }
}

在这里插入图片描述

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁棒最小二乘支持向量机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值