Skip to content

Commit 45e24ae

Browse files
committed
min_stack
1 parent 7e1bb67 commit 45e24ae

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
138138
#### [152. maximum product subarray](https://github.com/hitzzc/go-leetcode/tree/master/maximum_product_subarray)
139139
#### [153. find minimum in rotated sorted array](https://github.com/hitzzc/go-leetcode/tree/master/find_minimum_in_rotated_sorted_array)
140140
#### [154. find minimum in rotated sorted array II](https://github.com/hitzzc/go-leetcode/tree/master/find_minimum_in_rotated_sorted_array_II)
141+
#### [155. Min Stack](https://github.com/hitzzc/go-leetcode/tree/master/min_stack)
141142

142143

143144

min_stack/min_stack.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stack>
2+
using namespace std;
3+
4+
class MinStack {
5+
public:
6+
7+
MinStack() {
8+
9+
}
10+
11+
void push(int x) {
12+
normal_stack.push(x);
13+
if (min_stack.empty() || x <= min_stack.top()) min_stack.push(x);
14+
return;
15+
}
16+
17+
void pop() {
18+
int t = normal_stack.top();
19+
normal_stack.pop();
20+
if (t == min_stack.top()) min_stack.pop();
21+
return;
22+
}
23+
24+
int top() {
25+
return normal_stack.top();
26+
}
27+
28+
int getMin() {
29+
return min_stack.top();
30+
}
31+
private:
32+
stack<int> normal_stack;
33+
stack<int> min_stack;
34+
};

0 commit comments

Comments
 (0)