[LeetCode]155. Min Stack

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

[LeetCode]155. Min Stack

题目描述

这里写图片描述

思路

两个vector
一个vector存储栈
另一个vector存储到目前为止最小值的下标

代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        nums = {};
        mins = {};
        minNum = INT_MAX;
    }

    void push(int x) {
        if (x < minNum) {
            minNum = x;
            mins.push_back(nums.size());
        }
        nums.push_back(x);
    }

    void pop() {
        nums.pop_back();
        if (mins.back() >= nums.size())
            mins.pop_back();
        if (nums.size())
            minNum = nums[mins.back()];
        else
            minNum = INT_MAX;
    }

    int top() {
        return nums.back();
    }

    int getMin() {
        return minNum;
    }
private:
    vector<int> nums;
    vector<int> mins;
    int minNum;
};

int main() {
    MinStack minStack;
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    cout << minStack.getMin() << endl;
    minStack.pop();
    cout << minStack.top() << endl;
    cout << minStack.getMin() << endl;

    system("pause");
    return 0;
}
LeetCode力扣 155. 最小栈 Min Stack 题解代码 JavaScript 问题https://leetcode-cn.com/problems/min-stack/ 练习使用JavaScript解答 /** * initialize your data structure here. */ var MinStack = function() { this.stack = []; this.order = []; }; /** * @param {number} x * @return {void} */ MinStack.prototype.. 阅读详情

相关推荐

LeetCode 155. Min Stack 题解

本题是栈的经典应用问题,主要考察对栈数据结构的理解和使用。通过使用辅助栈,我们可以在常数时间内检索到最小元素。辅助栈的核心思想是:使用两个栈,一个主栈用于存储所有元素,一个辅助栈用于存储当前的最小元素,当推入元素时,将当前最小元素压入辅助栈,当弹出元素时,同时弹出主栈和辅助栈的栈顶元素。这种方法不仅适用于最小栈问题,还可以应用于许多其他需要在常数时间内检索到特定元素的问题。掌握辅助栈的使用,对于解决这类问题非常重要。

cannonjinx的博客 3076

LeetCode(155) Min Stack

题目如下: 分析如下: 我的代码:

BUILD 7457

[leetcode] 155. Min Stack

Description Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top...

Learning from the mistakes 256

Leetcode155. Min Stack

设计一个栈,除了栈特有的功能,入栈、出栈、查看栈顶元素,还需要增加一个功能,得到当前栈里边最小的元素。

magic_jiayu的博客 321

leetcode 155 Min Stack C++

用一个最小栈去维护最小值。 最小栈只入栈比当前值小的值。 因为在这个最小值后面进来的所有值中,最小值一直是这一个。 class MinStack { private: stack stk; stack min; public: /** initialize your data structure here. */ MinStack() {

wcxdell的专栏 1230

leetcode c++】155 Min stack

Min stack Design a stack that supports push, pop,top, and retrieving the minimum element in constant time.   push(x) -- Push element x onto stack. pop() -- Removes the element on top of thestack.

lif的博客 828

Leetcode 155. Min Stack

今天做了leetcode的第155题。 题目要求我们在自己设计的数据结构中能够随时提取出所有数据中的最小值,所以如何在每次push数据的时候能够保存最小值是关键,并且需要注意的是如果我们pop出了最小值,还要能够找到下一个最小值,所以如何保留和追踪剩下的所有数据中的最小值是关键。 在discussion中排名第二的方法比较巧妙:如果我们push的当前值比最小值要小,那就先把最小值pu...

dengyao9100的博客 159

leetcode155——Min Stack

题目大意:设计一个能在常数时间内检索到最小元素的栈 分析:两种方法。 方法一:使用pair<min,value>,只要在进栈时让min维护着当前栈中的最小值即可。 方法二:使用辅助栈,同步维护当前栈的最小值。当进栈元素小于辅助栈栈顶时,辅助栈压入进栈元素,否则仍然压入辅助栈栈顶元素。 代码: class MinStack { stack<pair<int...

tzyshiwolaogongya的博客 162

[leetcode]155. Min Stack

[leetcode]155. Min Stack Analysis 周末要吃鸡~—— [坚持吖~] Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop...

weixin_32135877的博客 181

leetcode 155 Min Stack

#include<stack> #include<limit.h> class MinStack { public: stack<int> stack1; /** initialize your data structure here. */ MinStack() { } void push(int x...

清风吹斜阳 149

Leetcode +155 Min Stack

Leetcode +155 Min Stack 题目描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top o...

qq_43271202的博客 206

LeetCode 155 Min Stack

题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。要求调用min ,push, pop的时间复杂度均为O(1) 解题思路 一般求解线性结构中的求中最小元素,遍历一遍即可求出。 但如果这样做不满足本题要求:时间复杂度O(1)。 且栈结构,只能访问栈顶,否则破坏了栈结构。 因此必须在每次压栈的时候,都要确定此时栈中的最小元素。可以开个辅助数组保存。 代码: class Solution { public: stack<int>s1,s2; // 一个存数据,

逗逗飞的专栏 667

LeetCode 155. Min Stack C++

Problem 题目链接 Solution 这题的题意一开始看错了 我看题目还以为让实现最小堆(写这句话的时候突然反应过来堆的英文是heap QAQ) 其实题目就是让你实现一个普通的栈,有常规的push、pop、top操作 另外让你在o(1)时间内实现查找栈中的最小元素 那么,因为栈是FILO的数据结构,我们只需另外开一个栈,来记录到某个位置时栈中的最小元素 拿入栈的元素和另外这个辅助栈的栈顶元素比较 若入栈元素小,则成为新的栈中最小元素 否则要把原来的栈顶元素再一次入栈,表示到这个位置为止的最小值 cla

dong的博客 281

LeetCode C++ 155. Min StackStack】简单

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum element in

memcpy0的博客 489

[LeetCode 155] Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Ge...

ExcitedZhang的博客 124

LeetCode155. Min Stack

155. Min Stack Description: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of...

Freemanzxp 334

LeetCode 155Min Stack

题目描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum elemen

iCode_girl的博客 169
上一篇: [LeetCode]190. Reverse Bits
下一篇: [LeetCode]532. K-diff Pairs in an Array
charon____
博客等级 码龄11年 3粉丝 202原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值