Skip to content

Commit 8566ece

Browse files
author
lucifer
committed
feat(ml): cpp code
1 parent 13dcaad commit 8566ece

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

problems/80.remove-duplicates-from-sorted-array-ii.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ for (int i = 0; i < len; i++) {
8484

8585
## 代码
8686

87-
代码支持: Python
87+
代码支持: Python, CPP
8888

8989
Python Code:
9090

@@ -101,6 +101,21 @@ class Solution:
101101
return i
102102
```
103103

104+
CPP Code:
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int removeDuplicates(vector<int>& A) {
110+
int j = 0;
111+
for (int i = 0; i < A.size(); ++i) {
112+
if (j - 2 < 0 || A[j - 2] != A[i]) A[j++] = A[i];
113+
}
114+
return j;
115+
}
116+
};
117+
```
118+
104119
**复杂度分析**
105120
106121
- 时间复杂度:$$O(N)$$
@@ -120,8 +135,6 @@ class Solution:
120135
121136
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghlu0vrcvlj318c0se0wm.jpg)
122137
123-
124-
125138
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
126139
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
127140
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

problems/84.largest-rectangle-in-histogram.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ https://leetcode-cn.com/problems/largest-rectangle-in-histogram/
1414

1515
![](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltx9kgd2j305805oa9z.jpg)
1616

17-
1817
图中阴影部分为所能勾勒出的最大矩形面积,其面积为  10  个单位。
1918

2019
```
@@ -152,6 +151,10 @@ class Solution:
152151

153152
### 代码
154153

154+
代码支持: Python,CPP
155+
156+
Python Code:
157+
155158
```python
156159
class Solution:
157160
def largestRectangleArea(self, heights: List[int]) -> int:
@@ -163,6 +166,29 @@ class Solution:
163166
return ans
164167
```
165168

169+
CPP Code:
170+
171+
```cpp
172+
class Solution {
173+
public:
174+
int largestRectangleArea(vector<int>& A) {
175+
A.push_back(0);
176+
int N = A.size(), ans = 0;
177+
stack<int> s;
178+
for (int i = 0; i < N; ++i) {
179+
while (s.size() && A[s.top()] >= A[i]) {
180+
int h = A[s.top()];
181+
s.pop();
182+
int j = s.size() ? s.top() : -1;
183+
ans = max(ans, h * (i - j - 1));
184+
}
185+
s.push(i);
186+
}
187+
return ans;
188+
}
189+
};
190+
```
191+
166192
**复杂度分析**
167193
168194
- 时间复杂度:$$O(N)$$

problems/85.maximal-rectangle.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ https://leetcode-cn.com/problems/maximal-rectangle/
5757

5858
## 代码
5959

60+
代码支持:Python, CPP
61+
62+
Python Code:
63+
6064
```python
6165
class Solution:
6266
def largestRectangleArea(self, heights: List[int]) -> int:
@@ -84,6 +88,39 @@ class Solution:
8488

8589
```
8690

91+
CPP Code:
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
int maximalRectangle(vector<vector<char>>& A) {
97+
if (A.empty() || A[0].empty()) return 0;
98+
int ans = 0, M = A.size(), N = A[0].size();
99+
vector<int> left(N, 0), right(N, N), height(N, 0);
100+
for (int i = 0; i < M; ++i) {
101+
int curLeft = 0, curRight = N;
102+
for (int j = 0; j < N; ++j) height[j] = A[i][j] == '1' ? height[j] + 1 : 0;
103+
for (int j = 0; j < N; ++j) {
104+
if (A[i][j] == '1') left[j] = max(left[j], curLeft);
105+
else {
106+
left[j] = 0;
107+
curLeft = j + 1;
108+
}
109+
}
110+
for (int j = N - 1; j >= 0; --j) {
111+
if (A[i][j] == '1') right[j] = min(right[j], curRight);
112+
else {
113+
right[j] = N;
114+
curRight = j;
115+
}
116+
}
117+
for (int j = 0; j < N; ++j) ans = max(ans, (right[j] - left[j]) * height[j]);
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
87124
**复杂度分析**
88125
89126
- 时间复杂度:$$O(M * N)$$

problems/86.partition-list.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ https://leetcode-cn.com/problems/partition-list/
5252
5353
## 代码
5454

55-
- 语言支持: Javascript,Python3
55+
- 语言支持: Javascript,Python3, CPP
5656

5757
```js
5858
/**
@@ -129,6 +129,31 @@ class Solution:
129129
return first_node.next
130130
```
131131

132+
CPP Code:
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
ListNode* partition(ListNode* head, int x) {
138+
ListNode dummy, geHead, *ltTail = &dummy, *geTail = &geHead;
139+
while (head) {
140+
auto p = head;
141+
head = head->next;
142+
if (p->val < x) {
143+
ltTail->next = p;
144+
ltTail = p;
145+
} else {
146+
geTail->next = p;
147+
geTail = p;
148+
}
149+
}
150+
ltTail->next = geHead.next;
151+
geTail->next = NULL;
152+
return dummy.next;
153+
}
154+
};
155+
```
156+
132157
**复杂度分析**
133158
134159
- 时间复杂度:$$O(N)$$

0 commit comments

Comments
 (0)