Skip to content

Commit 8e06dd6

Browse files
committed
[ADD] LeetCode 155 MinStack
- Medium
1 parent 3423dcb commit 8e06dd6

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

LeetCode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
|:----:|:-------------------------------------------------------------------:|:----------------------------------:|:----:|:--------------------------------------------------------------------------------:|:---------------------------------------:|
88
| 1 | [TwoSum](https://leetcode.com/problems/two-sum) | [cpp](source/TwoSum.cpp) | 2 | [PalindromeNumber](https://leetcode.com/problems/palindrome-number) | [cpp](source/PalindromeNumber.cpp) |
99
| 3 | [ValidParentheses](https://leetcode.com/problems/valid-parentheses) | [cpp](source/ValidParentheses.cpp) | 4 | [MiddleOfTheLinkedList](https://leetcode.com/problems/middle-of-the-linked-list) | [cpp](source/MiddleOfTheLinkedList.cpp) |
10-
| 5 | [PlusOne](https://leetcode.com/problems/plus-one) | [cpp](source/PlusOne.cpp) | | | |
10+
| 5 | [PlusOne](https://leetcode.com/problems/plus-one) | [cpp](source/PlusOne.cpp) | 6 | [MinStack](https://leetcode.com/problems/min-stack) | [cpp](source/MiddleOfTheLinkedList.cpp) |
1111

1212
</details>

LeetCode/source/MinStack.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// PalindromeNumber
2+
// 2022.08.10
3+
// Medium
4+
class MinStack
5+
{
6+
public:
7+
stack<int> st;
8+
stack<int> minSt;
9+
10+
void push(int val)
11+
{
12+
st.push(val);
13+
if (minSt.size() == 0)
14+
{
15+
minSt.push(val);
16+
}
17+
else if (val <= minSt.top())
18+
{
19+
minSt.push(val);
20+
}
21+
}
22+
23+
void pop()
24+
{
25+
int top = st.top();
26+
if (top == minSt.top())
27+
{
28+
minSt.pop();
29+
}
30+
st.pop();
31+
}
32+
33+
int top()
34+
{
35+
return st.top();
36+
}
37+
38+
int getMin()
39+
{
40+
return minSt.top();
41+
}
42+
};

0 commit comments

Comments
 (0)