当前位置:网站首页>Sword finger offer 30 Stack containing min function
Sword finger offer 30 Stack containing min function
2022-06-28 08:17:00 【wy_ forty-three million four hundred and thirty-one thousand ei】
The finger of the sword Offer 30. contain min Function of the stack
The difficulty is simple 272
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.
class MinStack {
/** initialize your data structure here. */
Stack<Integer> stack1;
Stack<Integer> stack2;
public MinStack() {
stack1=new Stack<>();
stack2=new Stack<>();
}
// push(x) function : The focus is to keep the stack B The element is Not strictly descending Of .
// take x Push to stack A ( namely A.add(x) );
// if ① Stack B It's empty or ② x Less than or equal to Stack B Top element of , Will x Push to stack B ( namely B.add(x) ).
public void push(int x) {
stack1.add(x);
if(stack2.isEmpty() || x<=stack2.peek()){
stack2.add(x);
}
}
//pop() function : The focus is to keep the stack A, B Of Element consistency .
// Execution stack A Out of the stack ( namely A.pop() ), Record the out of stack element as y ;
// if y Equal stack B Top element of , Then execute stack B Out of the stack ( namely B.pop() )
public void pop() {
//stack1.pop(); Redundant eject
if(stack1.pop().equals(stack2.peek())){
stack2.pop();
}
}
//top() function : Directly back to the stack A The top element of the stack , Return A.peek() .
public int top() {
return stack1.peek();
}
//min() function : Directly back to the stack B The top element of the stack , Return B.peek() .
public int min() {
return stack2.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.min();
*/
边栏推荐
- The micro kernel zephyr is supported by many manufacturers!
- Oracle view all tablespaces in the current library
- Solve NPM err! Unexpected end of JSON input while parsing near
- Is it reliable for securities companies to register and open accounts? Is it safe?
- Trigonometric transformation formula
- Study notes 22/1/17
- PLSQL installation under Windows
- B_QuRT_User_Guide(27)
- How to choose an account opening broker? Is it safe to open an account online?
- 22/02/14 study notes
猜你喜欢

Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation

sql分析(查询截取分析做sql优化)

22/02/14 study notes

图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换

Why MySQL cannot insert Chinese data in CMD

Prometheus monitoring (I)

Ambari (VIII) --- ambari integrated impala document (valid for personal test)

LeetCode之三步问题

B_QuRT_User_Guide(28)

SQL master-slave replication setup
随机推荐
设置网页的标题部分的图标
微内核Zephyr获众多厂家支持!
After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
App automated testing appium Tutorial Part 1 - advanced supplementary content
Modifying the SSH default port when installing Oracle RAC makes CRS unable to install
Connaissez - vous le protocole TCP (2)?
Solve NPM err! Unexpected end of JSON input while parsing near
Prometheus monitoring (I)
Airflow2.x distributed deployment DAG execution failure log cannot be obtained normally
Priority of JS operator
MySQL tablespace parsing
【学习笔记】线性基
Configuring MySQL multi instance master-slave synchronization for Linux
安装nrm后,使用nrm命令报错internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德
Ambari (IX) --- use expect to realize no interaction in ambri server setup phase (valid for personal test)
Jenkins' common build trigger and hook services (V)
About ASM disk space full, clean up ASM disk
The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion
NLP sequence can completely simulate human brain intelligence