[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.

  • 首次解题思路
    对每个结点增加一个最小值成员,该成员表示当该结点是栈顶元素时最小值的大小。结果提交之后LeetCode返回了Memory limited Error错误。
  • 首次尝试的代码
struct LinkNode
{
    int data;
    int min;
    LinkNode* next;
};

class MinStack {
public:
    MinStack():high(NULL){

    }

    void push(int x) {
        LinkNode* node = new LinkNode;
        node->data = x;
        if (high == NULL)
        {
            node->min = x;
        }
        else
        {
            int temp = x < high->min ? x : high->min;
            node->min = temp;
        }
        node->next = high;
        high = node;
    }

    void pop() {
        if (high)
        {
            LinkNode* temp = high;
            high = high->next;
            delete temp;
        }
    }

    int top() {
        if (high)
        {
            return high->data;
        }
    }

    int getMin() {
        return high->min;
    }

private:
    LinkNode* high;
};
  • 第二次解题思路
    用两个标准库的栈来维护这个MinStack,一个栈保存元素,另一个栈保存最小值。此时,可以对第二个栈的大小进行压缩。
  • 实现代码
/*************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/8 16:17
    *  @Status   : Accepted
    *  @Runtime  : 71 ms
*************************************************************/
#include <iostream>
#include <stack>
using namespace std;

class MinStack {
public:
    stack<int> num;
    stack<int> min;
    void push(int x) {
        if (min.size() == 0)
        {
            min.push(x);
        }
        else
        {
            if (x <= min.top())
            {
                min.push(x);
            }
        }
        num.push(x);
    }

    void pop() {
        if (!num.empty())
        {
            if (num.top() == min.top())
            {
                min.pop();
            }
            num.pop();
        }
    }

    int top() {
        if (!num.empty())
        {
            return num.top();
        }
    }

    int getMin() {
        if (!num.empty())
        {
            return min.top();
        }
    }
};
时间: 2024-10-12 05:46:07

[LeetCode] Min Stack的相关文章

LeetCode: 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 min

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 minim

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 All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

java中equals和==的区别

[LeetCode]–155. Min Stack 在这个问题中,我遇到了==和equals的问题,虽然试一下就能得出结果,但是我想弄明白. java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比较的是他们的值. 2.复合数据类型(类) 当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the

几个面试经典算法题Java解答

题目一: public class testClockwiseOutput {      //顺时针打印一个矩阵           @Test      public void test(){          int[][] num = new int[100][100];          int n = 4;          int count =1;                   for(int i=0;i<n;i++){              for(int j =0;j

[LeetCode]42.Trapping Rain Water

[题目] Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example,  Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represente

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret