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

带有minimum element属性的stack操作。

class MinStack {
    public Node node = null;
    public void push(int x) {
        if(node == null) {
            node = new Node(x);
            node.min = x;
        }
        else {
            Node tNode = new Node(x);
            tNode.next = node;
            node = tNode;
            node.min = node.next.min < x ? node.next.min : x;
        }
    }

    public void pop() {
        node = node.next;
    }

    public int top() {
        return node.val;
    }

    public int getMin() {
        return node.min;
    }
}
class Node {
    int val;
    int min;
    Node next;
    public Node(int val) {
        this.val = val;
    }

}

好久没1A了。

时间: 2024-08-07 00:14:31

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

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 不知道哪里不对,留待。

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

[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