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 minimum element in the stack.

思路:

本题目的解法是用到了两个栈,一个用来存元素,另一个用来存最小元素,元素入栈时和minStack栈里面的栈顶元素相比,小于栈顶元素则存入,大于栈顶元素则栈顶元素(当前元素中的最小值)入栈。其中,需要注意的是元素出栈时,要随时更新当前栈中的最小元素min=minStack.top()

代码:

 Stack<Integer> st = new Stack<Integer>();
	Stack<Integer> stMin = new Stack<Integer>();
	int min = 0;

	public void push(int x) {
		if (!st.isEmpty()) {
			if (min > x) {
				st.push(x);
				stMin.push(x);
				min = x;
			} else {
				st.push(x);
				stMin.push(min);
			}

		} else {
			st.push(x);
			stMin.push(x);
			min = x;
		}
	}

	public void pop() {
		st.pop();
		stMin.pop();
		if (!stMin.isEmpty())//元素出栈时要记着更新当前栈中的最小值
			min = stMin.peek();

	}

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

	public int getMin() {
		return stMin.peek();
	}

结果:

时间: 2024-10-03 21:41:30

leetcode_Min Stack的相关文章

51nod1289 stack

1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右).问足够长的时间之后,能剩下多少条鱼? Input 第1行:1个数N,表示鱼的数量(1 <= N <= 100000). 第2 - N + 1行:每行两个数A[i], 

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

About LAMP LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here i

Stack的pop和push操作

#include <stack> #include <cstdio> using namespace std; int main(){ stack<int> s; s.push(1); s.push(2); s.push(3); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top());

从头认识java-9.8 栈(Stack)

这一章节我们来讨论一下栈(Stack). 1.特性 先进后出,当一个元素压进栈里面,他就会处于栈的底部,然后,另一个再压进来,盖在原来的元素上面,原来的元素想出去,只有等上面的元素先顶出栈才有机会. 2.方法演示 package com.ray.ch09; import java.util.Arrays; import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Integ

C#中泛型容器Stack&lt;T&gt;的用法,以及借此实现&rdquo;撤销/重做&rdquo;功能

.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T>,双向链表的泛型实现,不支持索引;ISet<T>不允许被复制,他有2个实现,一个是HashSet<T>,不维持集合元素的排序,另一个是SortedSet<T>,支持集合元素的排序;IDictionary<TKey, TValue>是一个字典集合的泛型接口

Improving performance – A full stack problem

Improving performance – A full stack problem March 6, 2015 by ronald 4 Comments Improving the performance of a web system involves knowledge of how the entire technology stack operates and interacts. There are many simple and common tips that can pro

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 minimum e

[Java Basics] Stack, Heap, Constructor

Good about Java: friendly syntax, memory management[GC can collect unreferenced memory resources], object-oriented features, portability. Stack Stores method invocations, local variables(include object reference, but the object itself is still stored

使用Stack堆栈集合大数据运算

使用Stack堆栈集合大数据运算 package com.sta.to; import java.util.Iterator; import java.util.Stack; public class DaMax { public void jiaFa(String value1, String value2) { /** * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com * @author 小沫 */ // 把字符串用toCharArray拆成字符 char[] c1