LeetCode() Min Stack 不知道哪里不对,留待。

class MinStack {
public:
    MinStack()
    {
        coll.resize(2);
    }
    void push(int x) {
        if(index == coll.size()-1)
            coll.resize(2*coll.size());
        coll[++index]=x;
        if(x<min)
        {
            min=x;
            flag=index;
        }
    }

    void pop() {
            index--;

    }

    int top() {
        return coll[index];
    }

    int getMin() {
        if(index <flag&& index!=-1)
            {
                min=coll[0];
                for(int i=1;i<=index;i++)
                   if(coll[i]<min)
                   {
                       min=coll[i];
                       flag=i;
                   }
            }

        return min;
    }
private:
    vector<int> coll;
    int index=-1;
    int min=INT_MAX,flag;
};

  

时间: 2024-12-07 03:34:47

LeetCode() Min Stack 不知道哪里不对,留待。的相关文章

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question Solution 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

[LeetCode] Min Stack 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

LeetCode——Min Stack

Description: 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

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

[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

[LeetCode]Min Stack,解题报告

目录 目录 题目 思路1 AC代码 缺陷 思路2 AC代码 题目 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. getM

[leetcode] Min Stack @ Python

原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinStack: # @param x, an integer def __init__(self): self.stack1 = [] self.stack2 = [] # @return an integer def push(self, x): self.stack1.append(x) if len(

LeetCode[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 minim

LeetCode算法题-Min Stack(Java实现)

这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小元素的堆栈. push(x) - 将元素x推入堆栈. pop() - 删除堆栈顶部的元素. top() - 获取顶部元素. getMin() - 检索堆栈中的最小元素. 例如: MinStack minStack = new MinStack(); minStack.push(-2); minSta