tags: Leetcode c++ Development language rear end
Title link:155. Minimum stack

Design two stacks, one ST, a minst;
In the stack: ST is used normally, and gives MINST into the stack, Minst into St.TOP and Minst.top elements the least the minimum;
Out of the stack: ST out of the stack, MINST also puts the stack; because they are one or one;

class MinStack {
public:
MinStack() {
}
stack<int> st; / / Stack normal placed
stack<int> minSt; / / Stack of the smallest data
/ / Equivalent to normal stack into the stack
/ / The smallest value in the normal stack is also given to the smallest value.
void push(int val) {
st.push(val);
// Give the minimum stack into the stack, use the top of the stack of normal stacks and the minimum stack top, put more smaller into the smallest stack
if(minSt.empty()) // For the first time, the minimum stack is empty, directly into the top elements of the normal stack.
{
minSt.push(val);
}
else / / To judge the top and minimum stack top with the normal stack, put more small into the smallest stack
{
minSt.push(min(val,minSt.top()));
}
}
void pop() {
st.pop();
// Normal stack out of the stack, the minimum stack is also out, because they are one or one
minSt.pop();
}
int top() {
return st.top();
}
int getMin() {
return minSt.top();
}
};
/**
* 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();
*/
The result of unscrupulous. Come on ~! ...
Article Directory A, title Second, the solution 1. Rough 2. Optimize A, title description A support designed to push, pop, top operation, the stack can be retrieved and smallest elements in constant t...
Minimum stack Design a stack that supports PUSH, POP, TOP operations, and can retrieve the minimum element within a constant time. Push (x): Push element X into the stack. POP (): Delete elements on t...
Title description: Method one (ArrayList): Code: result: Method two (Deque): Use this method to pay attention to: eliminate the impact of the minimum number of each pop of the stack, so a mindq is est...
Design a stack that supports PUSH, POP, TOP operations, and can retrieve the minimum element within a constant time. Push (x) - Push the element X into the stack. POP () - Delete elements on the top o...
Article catalog topic: Solution 1: Array implementation Solution 2: Link list implementation Molution 3: 2 stacks Solution 4: topic: Solution 1: Array implementation Solution 2: Link list impleme...
Title description: Design a stack that supports push, pop, top operations, and can retrieve the smallest element in a constant time. Problem-solving ideas: Use two stacks s1, s2, one to store the real...
Write in front Stack usage Learning ideas, methods and methods Question details Understand the meaning of the question! ! ! ac code Dual stack leetcode 155. Minimal stack (c++) [C++] [Stack, vector] 1...
Article catalog All topic source code: [git address] (https://github.com/ch98road/leetcode) topic Program: All topic source code:Git address topic Program:...
topic Design a stack that supports PUSH, POP, TOP operations, and can retrieve the minimum element within a constant time. Push (x) - Push the element X into the stack. Pop () - Delete elements on the...