当前位置:网站首页>155. Min Stack
155. Min Stack
2022-06-22 13:17:00 【Sterben_ Da】
155. Min Stack
Easy
8363637Add to ListShare
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
MinStack()initializes the stack object.void push(int val)pushes the elementvalonto the stack.void pop()removes the element on the top of the stack.int top()gets the top element of the stack.int getMin()retrieves the minimum element in the stack.
Example 1:
Input ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); // return -2
Constraints:
-231 <= val <= 231 - 1- Methods
pop,topandgetMinoperations will always be called on non-empty stacks. - At most
3 * 104calls will be made topush,pop,top, andgetMin.
class MinStack:
"""
Refer to others' ideas for solving problems : Build an additional stack , The top of the stack is the minimum
Every time you insert data , If the minimum stack is empty or the value to be inserted is less than or equal to the top of the stack , Then press into the minimum stack
Every time you remove data , If the minimum stack top element is equal to the data to be removed , Then move out of the minimum stack
"""
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if len(self.min_stack) == 0 or self.min_stack[-1] >= val:
self.min_stack.append(val)
def pop(self) -> None:
val = self.stack.pop()
if len(self.min_stack) > 0 and val <= self.min_stack[-1]:
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()边栏推荐
- Rf5.0 new content quick view
- 241. Different Ways to Add Parentheses
- MySQL_数据处理之查询
- How to improve customer conversion rate on the official website
- Think PHP environment construction notes
- 记录阿里云ECS实例重启之后无法登录解决方法(亲身实践)
- 318. Maximum Product of Word Lengths
- 448. Find All Numbers Disappeared in an Array
- File download vulnerability & file read vulnerability & file delete vulnerability
- 342. Power of Four
猜你喜欢

SAP SPRO configure how to display the corresponding t-code

SICF批量激活服务节点

redis主备配置dockercompose版

剑指 Offer II 114. 外星文字典

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

MySQL_ Addition, deletion and modification of data processing

leetcode 968.监控二叉树

310. Minimum Height Trees

MySQL 5.7 + Navicat 下载安装教程(附安装包)

SNC processing failed SAP Router证书重新生成
随机推荐
vs code
建立自己的网站(5)
Secondary development of robotframework -- file parsing
Rce & Code Execution Vulnerability
47. Permutations II
MySQL 5.7 + Navicat 下载安装教程(附安装包)
MySQL中触发器
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
leetcode LCP 10. 二叉树任务调度
Tips of setup in robotframework
Detailed explanation of rules and ideas for advance sale of deposit
693. Binary Number with Alternating Bits
天坑专业学IC设计自学的话有公司会要吗
Leetcode 297 match de la semaine
JAXB element details
RobotFramework二次开发——Socket推送实时日志
基于SSM的小区垃圾分类和运输管理系统,高质量毕业论文范例(可直接使用),源码,数据库脚本,项目导入运行视频教程,论文撰写教程
2022-6-21os review group linking method
leetcode 1130. 叶值的最小代价生成树
190. Reverse Bits