Created
February 13, 2018 14:22
-
-
Save s4553711/f54a889f78373903cfffb4966068883a 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
class MinStack { | |
public: | |
stack<int> all; | |
stack<int> minsta; | |
/** initialize your data structure here. */ | |
MinStack() { | |
} | |
void push(int x) { | |
if (all.empty()) { | |
all.push(x); | |
minsta.push(x); | |
} else { | |
all.push(x); | |
if (x <= minsta.top()) minsta.push(x); | |
} | |
} | |
void pop() { | |
if (all.top() == minsta.top()) { | |
minsta.pop(); | |
} | |
all.pop(); | |
} | |
int top() { | |
return all.top(); | |
} | |
int getMin() { | |
return minsta.top(); | |
} | |
}; | |
/** | |
* Your MinStack object will be instantiated and called as such: | |
* MinStack obj = new MinStack(); | |
* obj.push(x); | |
* obj.pop(); | |
* int param_3 = obj.top(); | |
* int param_4 = obj.getMin(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment