剑指offer[c++] 包含min函数的栈

匿名 (未验证) 提交于 2019-12-03 00:37:01

剑指offer[c++] 包含min函数的栈

题目:

class Solution { public:     void push(int value) {         stack1.push(value);         if(stack2.empty())             stack2.push(value);         else              if(stack2.top()>=value)                 stack2.push(value);     }          void pop() {         if (stack1.top() == stack2.top())             stack2.pop();         stack1.pop();              }     int top() {         return stack1.top();              }     int min() {         return stack2.top();              }        private:     stack<int> stack1;     stack<int> stack2; // 辅助找最小值 };

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!