3.2---最小栈

//思路:入栈时不是最小值,永远都没机会成为最小值。

import java.util.Stack;
class MinStack {

    private Stack<Integer> stack = new Stack<Integer>();
    private Stack<Integer> minStack = new Stack<Integer>();

    public void push(int x) {
		stack.push(x);
		if(!minStack.empty())
		{
			int min = minStack.peek();
			if(x <= min)
			{
				minStack.push(x);
			}
		}
		else
		{
			minStack.push(x);
		}
    }

    public void pop() {

    	if(minStack.size() != 0 && ((int)stack.peek() == (int)minStack.peek()))
        {
        	minStack.pop();

        }
    	stack.pop();
    }

    public int top() {
        return (int)stack.peek();
    }

    public int getMin() {

        return (int)minStack.peek();
    }
}

  

时间: 2024-07-28 15:18:34

3.2---最小栈的相关文章

最小栈

思想分析: 实现方案: class MinStack { public: MinStack() {} void push(int x) { if (StackNum.empty()) { StackNum.push(x); StackMin.push(x); } else { if (x <= StackMin.top()) StackMin.push(x); StackNum.push(x); } } void pop() { if (StackMin.top() == StackNum.to

LeetCode OJ: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 e

【算法】最小栈

实现一个栈,带有出栈(pop).入栈(push).取最小元素(getMin)三个方法,且时间复杂度均为O(1). 初始想法: 创建 int min = -1,记录栈中最小元素的下标: 第一个元素进栈时,min = 0: 每当新元素进栈,让新元素与 min 下标位置的元素比较大小,min = 较小元素的下标: 调用 getMin 直接返回 min 的值. 这种方式进栈没有问题,而出栈时,若当前最小元素在栈顶并出栈了,用剩下哪个元素的下标顶替当前 min 就不得而知了.所以一旦最小元素的下标出栈,需

lintcode 中等题:Min stack 最小栈

题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义一个数组或者其他的存储最小值,第i个元素,表示栈中前i个元素的最小值. 定义两个ArrayList来存储栈,一个ArrayList存储当前栈中的元素,一个ArrayList存储最小栈,并且其第i个元素表示栈中前i个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元

[CareerCup] 3.2 Min Stack 最小栈

3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.

【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】

[155-Min Stack(最小栈)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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() –

最小栈的实现和优化

https://mp.weixin.qq.com/s/q5wtEXg_tC-wlyK1uMlJJA 最小栈 实现一个最小栈,一步一步优化,空间O(N) 时间O(1) . import java.util.ArrayList; import java.util.List; /** * @author xiaoshi on 2018/9/1. */ public class MinStack { private List<Integer> data = new ArrayList<Integ

LeetCode 155 Min Stack(最小栈)

翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop

【简单算法】39.最小栈

题目: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. getMin() -- 检索栈中的最小元素. 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3.

LeetCode155 栈&#183;最小栈(C++)

题目描述: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. getMin() -- 检索栈中的最小元素. 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回