leetcode MinStack

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

1, 记得stack的几个函数,isEmpty(),不能用null, equals,还有读stack最上面的数,但是不取出来的peek()

2,解法,用另外一个stack来计算最小的,最上面的永远是最小的

class MinStack {
    Stack<Integer> sta=new Stack<Integer>();
    Stack<Integer> minsta=new Stack<Integer>();
    public void push(int x) {
       sta.push(x);
       if(minsta.isEmpty()||x<=minsta.peek()){
           minsta.push(x);
       }
    }
    public void pop() {
        if(sta.peek().equals(minsta.peek())){
            minsta.pop();
        }
        sta.pop();
    }

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

    public int getMin() {
        return minsta.peek();
    }
}
时间: 2024-09-28 20:04:05

leetcode MinStack的相关文章

【leetcode】3 minstack

构建MinStack,实现一系列操作,包括push,pop,top,minstack(返回栈中最小元素) 思路:利用原始栈,不过这里需要两个栈,一个栈mystack用于存储元素,另一个栈otherstack元素由小到大排列 关键:mystack进行push(x)时,判断x与mystack的top()元素大小,始终将小的元素如栈minstack,这样mystack的top()始终是最小的 mystack的pop操作也是类似,当mystack与otherstack的top元素相同时,两个元素同时删除

【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 mini

【leetcode】Min Stack -- python版

题目描述: 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

LeetCode 155 Min Stack(最小栈)

翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop

【LeetCode 题解】Min Stack

同学推荐了LeetCode,相比其他的OJ,题量少,题目也比较经典,针对性特别强,适合练习. LeetCode相关的网上资源比较多,不过,看到题目一定要自己做一遍,然后再去参考其他人的解法. https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- P

【一天一道LeetCode】#155. Min Stack

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 el

LeetCode(Easy)--C++笔记

前言:这是关于LeetCode上面练习题C++的笔记,有些地方参考有网友的解题方法(可能有些参考没能注明,望谅解),如有需要改进的地方希望留言指教,多谢! 目录: ZigZag Conversion Reverse digits of an integer Implement atoi to convert a string to an integer Determine whether an integer is a palindrome Write a function to find th

[LeetCode]题解(python):155-Min Stack

题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两个数组来处理. 代码(python): 1 class MinStack(object): 2 def __init__(self): 3 """ 4 initialize your data structure here. 5 """ 6 self

第一次刷leetcode小结

LeetCode 上不会的 Reverse Integer Single Number II 斐波那契数列 非递归算法 Maximum Subarray 维持最大值 Integer to Roman Sort Colors Populating Next Right Pointers in Each Node 怎么样控制一层 Gray Code Generate Parentheses Binary Tree Postorder Traversal Best Time to Buy and Se