Java [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() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

解题思路:

主要是怎么保存最小值这个问题,每次在向栈中添加数据时,总是和当前的最小值比较,如果比当前的最小值小,那么则把当前的最小值压入栈中,表明当前栈中的最小值为当前最小值,然后再把新的数据压入栈中。每次弹出栈顶的元素时候,将弹出的数据与当前的最小值比较,如果相等,表明已经达到当前最小值所在的位置,下面的那个数是当前栈中的最小值,将其弹出,其值为新的最小值。

代码如下:

class MinStack {

	Stack<Integer> stack = new Stack<Integer>();
	int min = Integer.MAX_VALUE;

    public void push(int x) {
    	if(x <= min){
    		stack.push(min);
    		min = x;
    	}
    	stack.push(x);
    }    

    public void pop() {
    	int peek = stack.pop();
    	if(peek == min)
    		min = stack.pop();
    }

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

    public int getMin() {
        return min;
    }
}

  

时间: 2024-10-18 11:31:50

Java [Leetcode 155]Min Stack的相关文章

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

Java for 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() -- Get the top element.    getMin() -- Retrieve the

Leetcode 155. Min Stack 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() -- Get the top element. getMin() -- Retrieve the minimum e

[leetcode 155]min stack

1 题目 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 mini

Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include <map> #include <iostream> using namespace std; class MinStack { public: vector<int> vec; priority_queue<int,vector<int>,greater<

【LeetCode】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 mini

【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() -- Get the top element. getMin() -- Retrieve the minimum e

【一天一道LeetCode】#155. Min Stack

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 el

[LeetCode] 155. Min Stack_Easy tag: 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