Created
December 19, 2012 15:50
-
-
Save pdu/4337720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * http://www.17sotv.com/ 17sotv电影天堂 | |
| */ | |
| #include <vector> | |
| using namespace std; | |
| #include <stdint.h> | |
| class MyStack | |
| { | |
| public: | |
| MyStack(); | |
| ~MyStack(); | |
| void push(int32_t t); | |
| void pop(); | |
| int32_t get_min(); | |
| private: | |
| vector<int32_t> elems; | |
| vector<int32_t> mins; | |
| }; | |
| MyStack::MyStack() | |
| { | |
| } | |
| MyStack::~MyStack() | |
| { | |
| } | |
| void MyStack::push(int32_t t) | |
| { | |
| elems.push_back(t); | |
| if (mins.empty() || mins.back() >= t) | |
| { | |
| mins.push_back(t); | |
| } | |
| } | |
| void MyStack::pop() | |
| { | |
| if (elems.empty()) | |
| { | |
| return; | |
| } | |
| if (mins.back() == elems.back()) | |
| { | |
| mins.pop_back(); | |
| } | |
| elems.pop_back(); | |
| } | |
| int32_t MyStack::get_min() | |
| { | |
| if (mins.empty()) | |
| { | |
| throw; | |
| } | |
| return mins.back(); | |
| } | |
| int main() | |
| { | |
| MyStack s; | |
| s.push(5); | |
| printf("%d\n", s.get_min()); | |
| s.push(4); | |
| printf("%d\n", s.get_min()); | |
| s.pop(); | |
| printf("%d\n", s.get_min()); | |
| s.pop(); | |
| printf("%d\n", s.get_min()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment