Skip to content

Commit 6af2670

Browse files
add 155
1 parent cabce74 commit 6af2670

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ LeetCode
135135
|0131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) | c | [c++](./src/0131-Palindrome-Partitioning/0131.cpp) |[python](./src/0131-Palindrome-Partitioning/0131.py)|||Medium|
136136
|0132|[Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning/) | c | [c++](./src/0131-Palindrome-Partitioning/0131.cpp) |[python](./src/0131-Palindrome-Partitioning/0131.py)|||Medium|
137137
|0134|[Gas Station](https://leetcode.com/problems/gas-station/) | c | [c++](./src/0134-Gas-Station/0134.cpp) |[python](./src/0134-Gas-Station/0134.py)|||Medium|
138-
|0135|[Candy](https://leetcode.com/problems/gas-station/) | c | [c++](./src/0134-Gas-Station/0134.cpp) |[python](./src/0134-Gas-Station/0134.py)|||Medium|
138+
|0135|[Candy](https://leetcode.com/problems/gas-station/) | c | [c++](./src/0134-Gas-Station/0134.cpp) |[python](./src/0134-Gas-Station/0134.py)|||Hard|
139139
|0136|[Single Number](https://leetcode.com/problems/single-number/) | c | [c++](./src/0136-Single-Number/0136.cpp) |[python](./src/0136-Single-Number/0136.py)|||Easy|
140140
|0137|[Single Number II](https://leetcode.com/problems/single-number-ii/) | c | [c++](./src/0137-Single-Number-II/0137.cpp) |[python](./src/0137-Single-Number-II/0137.py)|||Medium|
141+
|0138|[Copy List with Random Pointer](https://leetcode.com/problems/single-number-ii/) | c | [c++](./src/0137-Single-Number-II/0137.cpp) |[python](./src/0137-Single-Number-II/0137.py)|||Medium|
141142
|0139|[Word Break](https://leetcode.com/problems/word-break/) | c | [c++](./src/0139-Word-Break/0139.cpp) |[python](./src/0139-Word-Break/0139.py)|||Medium|
142143
|0141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | c | [c++](./src/0141-Linked-List-Cycle/0141.cpp) |[python](./src/0141-Linked-List-Cycle/0141.py)|||Easy|
143144
|0142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | c | [c++](./src/0142-Linked-List-Cycle-II/0142.cpp) |[python](./src/0142-Linked-List-Cycle-II/0142.py)|||Medium|
@@ -152,6 +153,7 @@ LeetCode
152153
|0152|[Maximum Product Subarray](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | c | [c++](./src/0152-Maximum-Product-Subarray/0152.cpp) |[python](./src/0152-Maximum-Product-Subarray/0152.py)|||Medium|
153154
|0153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | c | [c++](./src/0153-Find-Minimum-in-Rotated-Sorted-Array/0153.cpp) |[python](./src/0153-Find-Minimum-in-Rotated-Sorted-Array/0153.py)|||Medium|
154155
|0154|[Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | c | [c++](./src/0154-Find-Minimum-in-Rotated-Sorted-Array-II/0154.cpp) |[python](./src/0154-Find-Minimum-in-Rotated-Sorted-Array-II/0154.py)|||Hard|
156+
|0155|[Min Stack](https://leetcode.com/problems/min-stack/) | c | [c++](./src/0155-Min-Stack/0155.cpp) |[python](./src/0155-Min-Stack/0155.py)|||Easy|
155157
|0162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | c | [c++](./src/0162-Find-Peak-Element/0162.cpp) |[python](./src/0162-Find-Peak-Element/0162.py)|||Medium|
156158
|0167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | c | [c++](./src/0167-Two-Sum-II-Input-array-is-sorted/0167.cpp) |[python](./src/0167-Two-Sum-II-Input-array-is-sorted/0167.py)|||Easy|
157159
|0169|[Majority Element](https://leetcode.com/problems/majority-element/) | c | [c++](./src/0169-Majority-Element/0169.cpp) |[python](./src/0169-Majority-Element/0169.py)|||Easy|

src/0155-Min-Stack/0155.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
2+
class MinStack
3+
{
4+
public:
5+
void push(int x)
6+
{
7+
s1.push(x);
8+
if (s2.empty() || x <= getMin()) s2.push(x);
9+
}
10+
void pop()
11+
{
12+
if (s1.top() == getMin()) s2.pop();
13+
s1.pop();
14+
}
15+
int top()
16+
{
17+
return s1.top();
18+
}
19+
int getMin()
20+
{
21+
return s2.top();
22+
}
23+
private:
24+
stack<int> s1;
25+
stack<int> s2;
26+
};

src/0155-Min-Stack/0155.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class MinStack:
2+
def __init__(self):
3+
"""
4+
initialize your data structure here.
5+
"""
6+
self.data = []
7+
self.min_val = float("inf")
8+
9+
def push(self, x):
10+
"""
11+
:type x: int
12+
:rtype: void
13+
"""
14+
if x <= self.min_val:
15+
self.data.append(self.min_val)
16+
self.min_val = x
17+
18+
self.data.append(x)
19+
20+
def pop(self):
21+
"""
22+
:rtype: void
23+
"""
24+
if self.data[-1] == self.min_val:
25+
self.data.pop()
26+
self.min_val = self.data[-1]
27+
self.data.pop()
28+
else:
29+
self.data.pop()
30+
31+
def top(self):
32+
"""
33+
:rtype: int
34+
"""
35+
return self.data[-1]
36+
37+
def getMin(self):
38+
"""
39+
:rtype: int
40+
"""
41+
return self.min_val

0 commit comments

Comments
 (0)