【Leetcode-Easy-155】 Min Stack
来源:互联网 发布:科尔沃折叠刀淘宝 编辑:程序博客网 时间:2024/06/06 09:02
【Leetcode-Easy-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 minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); –> Returns -3.
minStack.pop();
minStack.top(); –> Returns 0.
minStack.getMin(); –> Returns -2.
思路
双栈
程序
class MinStack { private LinkedList<Integer> dataStack = null; private LinkedList<Integer> minStack = null; /** initialize your data structure here. */ public MinStack() { dataStack = new LinkedList<>(); minStack = new LinkedList<>(); } public void push(int x) { dataStack.push(x); if (minStack.isEmpty()) minStack.push(x); else minStack.push(Math.min(minStack.peek(), x)); } public void pop() { if(!minStack.isEmpty()) minStack.pop(); if(!dataStack.isEmpty()) dataStack.pop(); } public int top() { return dataStack.peek(); } public int getMin() { return minStack.peek(); }}/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
声明:题目来自Leetcode.
阅读全文
0 0
- [Leetcode 155, Easy] Min Stack
- 【Leetcode-Easy-155】 Min Stack
- Easy 155题 Min Stack
- Leetcode 155. Min Stack (Easy) (cpp)
- LeetCode-Easy刷题(33) Min Stack
- LeetCode(155) Min Stack
- [leetcode 155] Min Stack
- leetCode #155 Min Stack
- LeetCode[155]Min Stack
- leetcode-155 Min Stack
- leetcode 155 Min Stack
- LeetCode[155]-Min Stack
- leetcode 155:Min Stack
- LeetCode---(155)Min Stack
- leetcode-155-Min Stack
- leetcode 155: Min Stack
- [leetcode] #155 Min Stack
- LeetCode 155 Min Stack
- python切片
- URI中 ? # & 的作用
- xadmin下设置“use_bootswatch = True”无效的原因
- TP框架中的M,D,C,A,I,S方法
- 关于a ++ 与++ a的问题
- 【Leetcode-Easy-155】 Min Stack
- SPI Flash Rom W25Q16 ----基于STC15
- java 覆盖写入
- linux下端口被占用的解决方法
- Angular4 & MVC方法调用
- VMware虚拟磁盘管理器用法帮助 用法:vmware-vdiskmanager.exe
- 谈“+”,你真的了解么?一个面试题引发的思考------2017.10.19
- Python出现: TypeError: expected string or buffer
- 搭建本地git服务器(前面Linux版本为:Ubuntu或Debian;后面Linux版本为:CentOs)