当前位置:网站首页>Leetcode-155-minimum stack
Leetcode-155-minimum stack
2022-07-27 22:12:00 【benym】
# LeetCode-155- Smallest stack
Design a support push ,pop ,top operation , And can retrieve the stack of the smallest elements in a constant time .
- push(x) —— Put the element x Push into the stack .
- pop() —— Delete the element at the top of the stack .
- top() —— Get stack top element .
- getMin() —— Retrieve the minimum elements 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]
explain :
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.Tips :
pop、topandgetMinThe operation is always in Non empty stack On the call .- If there is no intersection between two linked lists , return null.
- After returning the result , The two lists still have to keep their original structure .
- It can be assumed that there is no cycle in the whole linked list structure .
- The procedure is as satisfying as possible O(n) Time complexity , And only O(1) Memory .
# Their thinking
Method 1、 Two stacks :
Need a data stack , A minimum stack
The minimum stack always stores the current minimum
stay push While entering the data stack , Judge whether the minimum stack is empty or whether the new value is less than the top value of the minimum stack ,
If less than, add a new value to the minimum stack , If not less than, add to the top of the minimum stack ( That is, the last smallest element ) Enter the minimum stack
When you need to pop When , Pop up the minimum stack and data stack values at the same time
When you need to getMin when , The element at the top of the stack that returns the smallest stack is the current smallest element
When you need to get top when , Returns the top element of the data stack
# Java Code
class MinStack {
private Stack<Integer> stack_data;
private Stack<Integer> stack_min;
/** initialize your data structure here. */
public MinStack() {
stack_data = new Stack<>();
stack_min = new Stack<>();
}
public void push(int x) {
stack_data.push(x);
if(stack_min.size()==0||x<stack_min.peek()){
stack_min.push(x);
}else{
stack_min.push(stack_min.peek());
}
}
public void pop() {
stack_data.pop();
stack_min.pop();
}
public int top() {
return stack_data.peek();
}
public int getMin() {
return stack_min.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();
*/# Java Code 2
class MinStack {
private Deque<Integer> stackData;
private Deque<Integer> stackMin;
/** initialize your data structure here. */
public MinStack() {
stackData = new LinkedList();
stackMin = new LinkedList();
stackMin.push(Integer.MAX_VALUE);
}
public void push(int val) {
stackData.push(val);
stackMin.push(Math.min(val,stackMin.peek()));
}
public void pop() {
stackData.pop();
stackMin.pop();
}
public int top() {
return stackData.peek();
}
public int getMin() {
return stackMin.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/边栏推荐
- [numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
- Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
- Matlab draws the statistical rose chart of wind speed and direction
- Go language learning notes - mutex start go language from scratch
- 数组扩容、排序、嵌套语句应用
- 云原生微服务第三章之Haproxy+Keepalived
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
- Drawing three coordinate (axis) diagram with MATLAB
- Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
- 九天后我们一起,聚焦音视频、探秘技术新发展
猜你喜欢

It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years

MySQL execution process and order

Pythia: Facebook's latest open source visual and language multitasking learning framework

A lock faster than read-write lock. Don't get to know it quickly

光藕继电器

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

Lvs+kept highly available cluster

Pytoch distributed training

How to use Fiddler for weak network testing

Interview question: what are the functions of fail safe mechanism and fail fast mechanism
随机推荐
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
Are Transformers Effective for Time Series Forecasting?|填坑
[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
项目分析(哪些是it培训给不了)
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Monitor the running of server jar and restart script
An2021软件安装及基本操作(新建文件/导出)
[numerical analysis exercise] Jacobi iteration method of third-order matrix
2021-11-05 understanding of class variables and class methods
Understanding of L1 regularization and L2 regularization [easy to understand]
What is modcount in the source code? What's the effect
For 3nm and below processes, ASML new generation EUV lithography machine exposure
Interview questions that big companies need to prepare
Yyds dry goods inventory # solve the real problem of famous enterprises: cycle number comparison
数组扩容、排序、嵌套语句应用
枚举和注解
The source code of live broadcast app system, and the rotation diagram of upper and lower layers
Behind every piece of information you collect, you can't live without TA
8000 word explanation of OBSA principle and application practice
2022 2nd cyber edge cup cyber security competition Web