首页 > 代码库 > leetcode 155. Min Stack
leetcode 155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
1 class MinStack { 2 public: 3 void push(int x) 4 { 5 if (minS.empty() || x <= minS.top()) 6 minS.push(x); 7 S.push(x); 8 } 9 10 void pop() 11 {12 if (minS.top() == S.top())13 minS.pop();14 S.pop();15 }16 17 int top() 18 {19 return S.top(); 20 }21 22 int getMin() 23 {24 return minS.top(); 25 }26 27 private:28 stack<int> S;29 stack<int> minS;30 };
leetcode 155. Min Stack
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。