LeetCode-Min Stack-解题报告
来源:互联网 发布:君子知怕的论证过程 编辑:程序博客网 时间:2024/05/28 15:52
原题链接https://leetcode.com/problems/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() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
我用两个栈,一个栈Stack放原本的元素,另一个栈Min 存放最小值。
Stack的入栈情况不变。
Min栈
入栈情况。
如果栈为空,当入栈的时候,直接将元素push到Min
如果栈不为空,当入栈的元素<= Min.top(),入栈,反之则不入栈。
出栈情况
如果出栈元素和Min.top()相等,则Min.pop(),,其他情况Min不出栈。
因为栈的特点是FILO,所以当其他比Min.top()大的元素入栈是不会影响到最小值的。因此Min.top()总是Stack中当前的最小值。
class MinStack {public:void push(int x) {Stack.push(x);if (Min.empty())Min.push(x);else if (x <= Min.top())Min.push(x);}void pop() {if (Stack.top() == Min.top())Min.pop();Stack.pop();}int top() {return Stack.top();}int getMin() {return Min.top();}private:stack<int>Stack;stack<int>Min;};
0 0
- 【LeetCode】Min Stack 解题报告
- 【LeetCode】Min Stack 解题报告
- [LeetCode]Min Stack,解题报告
- LeetCode-Min Stack-解题报告
- [leetcode] 155. Min Stack 解题报告
- [Leetcode] 155. Min Stack 解题报告
- Min Stack 解题报告
- 解题报告: #115 Min Stack
- leetCode Min Stack解题分享
- leetcode解题方案--155--min stack
- LeetCode[stack]: Min Stack
- [leetcode][stack] Min Stack
- 【LeetCode】Implement Stack using Queues 解题报告
- LeetCode-Implement Stack using Queues-解题报告
- 【LeetCode】Implement Stack using Queues 解题报告
- leetcode解题之155# Min Stack Java版 (包含min函数的栈)
- 【Leetcode】Min Stack
- Leetcode: Min Stack
- SQL Server 跨服务器 不同数据库之间复制表的数据的方法!
- GRE写作必备句型
- import caffe时出错:can not find module skimage.io
- [深入学习C#]C#实现多线程的方式:Task——任务
- Mac 批量删除 .svn 文件
- LeetCode-Min Stack-解题报告
- 亲和串
- Java——集合工具类和jdk1.5有关集合的新特性
- 如何在spring官网上下载source code
- 算法思路
- 斯坦福《机器学习》Lesson4感想--1、Logistic回归中的牛顿方法
- ORA-00600 [3020] ORA-10560问题处理
- 【转】XmlDocument类
- 输入一个整数数组,判断该数组是不是某个二叉搜索树的前序遍历结果