包含min函数的栈

xiaoxiao2021-02-28  19

注意:题目不是实现一个栈,而是在栈的基础上实现一些操作

class Solution { public: void push(int value) { st.push(value); if (smin.empty()) smin.push(value); if (smin.top() > value) smin.push(value); } void pop() { if (smin.top() == st.top()) smin.pop(); st.pop(); } int top() { return st.top(); } int min() { return smin.top(); } private: stack<int> st; stack<int> smin; };
转载请注明原文地址: https://www.6miu.com/read-2250231.html

最新回复(0)