当前位置:网站首页>Leetcode points to offer 30 Stack containing min function
Leetcode points to offer 30 Stack containing min function
2022-06-13 09:22:00 【Shao_ sen】
The finger of the sword Offer 30. contain min Function of the stack
difficulty Simple
Defines the data structure of the stack , Please implement a in this type that can get the minimum elements of the stack min The function is in the stack , call min、push And pop The time complexity of O(1).
Example :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> return -3.
minStack.pop();
minStack.top(); --> return 0.
minStack.min(); --> return -2.
Tips :
- The total number of calls to each function shall not exceed 20000 Time
Answer key
This problem is to design a minimum stack , It has one more function than stack Min, How to design this function , I think the simpler thing is to design a stack of the same size , This stack mainly records the minimum value from the front to the present , Stack together .
For example, the example given
[“MinStack”,“push”,“push”,“push”,“min”,“pop”,“top”,“min”]
[[],[-2],[0],[-3],[],[],[],[]]
Three times push When , The two stacks are [-3, 0, -2] and [-3, -2, -2], The second is the minimum stack , Record the minimum value from the beginning to the present , such min The time complexity is O(1) 了
There is also a hole pop function , Because of joining min After the stack , If the smallest element pops up , We don't update min The value will appear bug, Such as now min There is... In the stack [-20, -10, 0], When we pop up -20, Recompression -14 When , If the previous stack is not updated min value , So this -14 It's like a stack , And pressed in -20.( These are code logic problems , Especially when designing multiple objects , Be sure to pay attention to logic , Otherwise there will be unexpected bug)
class MinStack {
Deque<Integer> deque;// Stack
Deque<Integer> minDeque;//min Stack
int min = Integer.MAX_VALUE;//min value
/** initialize your data structure here. */
public MinStack() {
// initialization
deque = new ArrayDeque<Integer>();
minDeque = new ArrayDeque<Integer>();
}
public void push(int x) {
deque.push(x);// Push
min = Math.min(min, x);// Compare the minimum
minDeque.push(min);// The minimum value is put on the stack
}
public void pop() {
if(!deque.isEmpty()){
// The queue is not empty
deque.removeFirst();// Because it uses a double ended queue deque,push It's to be put at the head of the team , namely first Location , So what pops up is first
minDeque.removeFirst();
if(!minDeque.isEmpty()){
//min Stack is not empty.
min = minDeque.getFirst();// Stack update min value
}else{
min = Integer.MAX_VALUE;// When min When the stack is empty , Update to maximum , Avoid using old values next time
}
}
}
public int top() {
return deque.getFirst();
}
public int min() {
return minDeque.getFirst();
}
}
/** * 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.min(); */
边栏推荐
- Library management system based on wechat applet Rar (thesis + source code)
- Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
- ROS2之OpenCV人脸识别foxy~galactic~humble
- 20211020 academician all drive system
- JUC atomic array
- C language: callback function
- Online debugging tool Arthas advanced
- Neo4j - CQL使用
- Lecture par lots de tous les fichiers vocaux sous le dossier
- How to build an aby framework and run an instance
猜你喜欢
final 原理
C language: deep understanding of character functions and string functions (1)
Redis fuzzy query batch deletion
Longadder of the source code of JUC atomic accumulator
Drill down to protobuf - Introduction
Class loading overview
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
类的加载概述
20211104 why are the traces of similar matrices the same
C/s model and P2P model
随机推荐
20211006 integral, differential and projection belong to linear transformation
Yolov5 face learning notes
Talking about acid of database
C language: callback function
LeetCode 5289. 公平分发饼干(DFS)
LeetCode 72. 编辑距离
C/s model and P2P model
【最全面详细解释】背包问题详解
阿里高级专家剖析 | Protocol的标准设计
Batch read all voice files under the folder
LeetCode 583. 两个字符串的删除操作
Figure introduction to database neo4j
JUC Unsafe
How to build an aby framework and run an instance
I have summarized the knowledge points of JS [intermediate and advanced] for you
C language: preprocessing in program environment
Alibaba senior experts analyze the standard design of protocol
Yolov5 model evaluation
Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
C language: file operation