155. Min Stack linked list implementation

https://leetcode.com/problems/min-stack/

Implement a stack so that it can perform top, pop, push operations, while also being able to return the current minimum value
--------------------------- --------------------------------
Input
-----------------
["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"]
[[], [-2], [0], [-3] , [], [], [], []]
------------------------------------- ---------------------------------
Output
----------------
[null, null, null, null, -3, null, 0, -2]
Explanation
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

analysis:

Set two stacks, one to save the inserted value and one to save the minimum value of the current element after insertion

1. Implement a stack with a linked list

class MinStack {
public:
    /** initialize your data structure here. */
    struct StackNode{
        int val;
        StackNode* next;
        StackNode(int val){
            this->val = val;
        }
    };
    
    StackNode* top_stack;
    StackNode* min_stack;
    
    MinStack(){
        top_stack=NULL;
        min_stack=NULL;
    }
    
    void push(int x) {
        if(!top_stack || x <= min_stack->val)
        {   
            StackNode* temp1 = new StackNode(x);
            temp1->next = min_stack;
            min_stack = temp1;
        }
        StackNode* temp = new StackNode(x);
        temp->next = top_stack;
        top_stack = temp;
    }
    
    void pop() {
        if(!top_stack)
            return;
        
        if(top_stack->val == min_stack->val)
        {
            StackNode* temp = min_stack;
            min_stack = min_stack->next;
            delete temp;
        }
        
        StackNode* temp1 = top_stack;
        top_stack = top_stack->next;
        delete temp1;

    }
    
    int top() {
        return top_stack->val;
    }
    
    int getMin() {
        return min_stack->val;
    }
};

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

2. Use the built-in vector

class MinStack {
public:
    /** initialize your data structure here. */
    vector<int> top_stack;
    vector<int> min_stack;
    
    void push(int x) {
        if(top_stack.empty() || x <= min_stack.back())
        {
            top_stack.push_back(x);
            min_stack.push_back(x);
        }
        else
        {
            top_stack.push_back(x);
        }
    }
    
    void pop() {
        if(top_stack.back() == min_stack.back())
        {
            top_stack.pop_back();
            min_stack.pop_back();
        }
        else
            top_stack.pop_back();
    }
    
    int top() {
        return top_stack.back();
    }
    
    int getMin() {
        return min_stack.back();
    }
};

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

Guess you like

Origin www.cnblogs.com/qiulinzhang/p/12724910.html