利用最小栈实现栈存取数据时间复杂度在O(1)
作者:互联网
#include <iostream> #include <string> using namespace std; template<typename T> class StackWithMin { stack data; stack minStack; void push(const T& val) { data.push(val); if (minStack.size() == 0 || val < minStack.top()) { minStack.push(val); } else { minStack.push(minStack.top()); } } void pop() { if (data.size() == 0) { return; } data.pop(); minStack.pop(); } T& min() { if (minStack.size() == 0) { return; } return minStack.top(); } };
标签:minStack,val,复杂度,最小,pop,存取数据,push,return,data 来源: https://www.cnblogs.com/dongma/p/13921793.html