leetcode_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 the stack.

top() -- Get the top element.

getMin() -- Retrieve the minimum element in the stack.

//vs2012测试代码
//相比传统stack(记为stk),为了记录最小值,需要再开一个最小值栈min。
//需要注意的是:重复出现的最小值必须重复进min,不然出stk的时候,min可能会为空出错
#include<iostream>
#include<stack>

using namespace std;

class MinStack {
stack<int> min;
stack<int> temp;
public:
    void push(int x)
	{
		temp.push(x);
		if( min.empty() || x<=min.top() )
			min.push(x);
    }

    void pop()
	{
		if( temp.top()==min.top() )
		{
			temp.pop();
			min.pop();
		}
		else
			temp.pop();
    }

    int top()
	{
		return temp.top();
    }

    int getMin()
	{
		return min.top();
    }
};

int main()
{
	MinStack lin;
	for(int i=0; i<5; i++)
	{
		int x;
		cin>>x;
		lin.push(x);
	}
	cout<<lin.getMin()<<endl;
}
//方法一:自测Accepted
//相比传统stack(记为stk),为了记录最小值,需要再开一个最小值栈min。
//需要注意的是:重复出现的最小值必须重复进min,不然出stk的时候,min可能会为空出错
class MinStack {
stack<int> min;
stack<int> temp;
public:
    void push(int x)
	{
		temp.push(x);
		if( min.empty() || x<=min.top() )
			min.push(x);
    }

    void pop()
	{
		if( temp.top()==min.top() )
		{
			temp.pop();
			min.pop();
		}
		else
			temp.pop();
    }

    int top()
	{
		return temp.top();
    }

    int getMin()
	{
		return min.top();
    }
};
时间: 2024-08-01 00:19:52

leetcode_155_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