Skip to content

Commit ba447f4

Browse files
author
cpppy
authored
Create 155_Min_Stack.cc
1 parent ab52800 commit ba447f4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

155_Min_Stack.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class MinStack {
2+
public:
3+
/** initialize your data structure here. */
4+
MinStack() {
5+
6+
}
7+
8+
void push(int x) {
9+
s.push(x);
10+
if(s_min.empty()||x<=s_min.top()){
11+
s_min.push(x);
12+
}
13+
}
14+
15+
void pop() {
16+
if(s.top()==s_min.top())
17+
{
18+
s_min.pop();
19+
}
20+
s.pop();
21+
22+
}
23+
24+
int top() {
25+
return s.top();
26+
}
27+
28+
int getMin() {
29+
return s_min.top();
30+
}
31+
32+
private:
33+
stack<int> s;
34+
stack<int> s_min;
35+
};
36+
37+
/**
38+
* Your MinStack object will be instantiated and called as such:
39+
* MinStack obj = new MinStack();
40+
* obj.push(x);
41+
* obj.pop();
42+
* int param_3 = obj.top();
43+
* int param_4 = obj.getMin();
44+
*/

0 commit comments

Comments
 (0)