155. Min Stack 链表实现

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

实现一个栈,使其能够 top, pop, push 操作,同时还要能够返回当前的最小值
-----------------------------------------------------------
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

分析:

设置两个栈,一个保存插入值,一个保存当前元素插入后的最小值

1. 用链表实现一个栈

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. 使用自带的 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();
 */

猜你喜欢

转载自www.cnblogs.com/qiulinzhang/p/12724910.html