#include<iostream> #include<stack> using namespace std; class Solution { public: stack<int>a, b; int min() { if(!b.empty()) return b.top(); else return -1; } int pop() { if (!a.empty()) { int x = a.top(); if (x == b.top()) b.pop(); a.pop(); return x; } else return -1; } void push(int x) { if (a.empty() && b.empty()) { a.push(x); b.push(x); } else if(x<b.top()){ b.push(x); a.push(x); } else { a.push(x); } } }; int main() { Solution s; s.push(1); cout << s.min() << endl; s.push(0); cout << s.min() << endl; s.push(3); cout << s.min() << endl; s.pop(); cout << s.min() << endl; s.pop(); cout << s.min() << endl; s.pop(); cout << s.min() << endl; return 0; }
The sword refers to offer30
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324775378&siteId=291194637
Ranking