155. Minimum stack (C ++ implementation)

tags: Leetcode  c++  Development language  rear end

Article catalog

topic

Title link:155. Minimum stack


Think

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();
 */

Intelligent Recommendation

155. Minimum stack -Record (C ++)

The result of unscrupulous. Come on ~!  ...

[C ++] [stack, vector] 155. Minimum stack

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...

Leetcode 155. Minimum Stack - C ++ - Auxiliary Stack

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...

LeetCode 155 minimum stack (Java implementation)

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...

Minimum stack -leetcode 155 -js implementation

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...

More Recommendation

155. Minimum stack (Java implementation) - Leetcode

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...

Leetcode question 155, minimum stack C++

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...

leetcode-155. Minimum stack brushing notes (C++)

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...

C language reconstruction [155] minimum stack

Article catalog All topic source code: [git address] (https://github.com/ch98road/leetcode) topic Program: All topic source code:Git address topic Program:...

[Stack] 155. Minimum Stack

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top