题目

[Leetcode]最小栈 

代码

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack():nums(),sorted() 
    {
        
    }
    
    void push(int x) 
    {
        sorted[x]++;
        nums.push(x);
    }
    
    void pop() 
    {
        sorted[nums.top()]--;
        if(sorted[nums.top()]==0)
            sorted.erase(sorted.find(nums.top()));
        nums.pop();
    }
    
    int top() 
    {
        return nums.top();
    }
    
    int getMin() 
    {
        auto ptr= sorted.begin();
        return ptr->first;
    }
    std::map<int,int> sorted;
    std::stack<int> nums;
};

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

 

相关文章:

  • 2020-02-09
  • 2021-06-30
  • 2022-12-23
  • 2021-05-13
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-24
  • 2022-01-02
  • 2021-12-30
  • 2022-12-23
  • 2021-06-19
  • 2021-04-12
  • 2021-08-13
相关资源
相似解决方案