@@ -85,7 +85,7 @@ class Solution:
85
85
86
86
### 代码
87
87
88
- - 语言支持: Python, javascript
88
+ - 语言支持: Python, javascript, CPP
89
89
90
90
javascript code:
91
91
@@ -133,6 +133,26 @@ class Solution:
133
133
return res
134
134
```
135
135
136
+ CPP Code:
137
+
138
+ ``` cpp
139
+ class Solution {
140
+ public:
141
+ int longestValidParentheses(string s) {
142
+ stack<int > st;
143
+ st.push(-1);
144
+ int ans = 0;
145
+ for (int i = 0; i < s.size(); ++i) {
146
+ if (s[ i] == ')' && st.top() != -1 && s[ st.top()] == '(') {
147
+ st.pop();
148
+ ans = max(ans, i - st.top());
149
+ } else st.push(i);
150
+ }
151
+ return ans;
152
+ }
153
+ };
154
+ ```
155
+
136
156
**复杂度分析**
137
157
138
158
- 时间复杂度:$$O(N)$$
@@ -154,7 +174,9 @@ class Solution:
154
174
155
175
### 代码
156
176
157
- 代码支持:Java,Python
177
+ 代码支持:Java,Python3, CPP
178
+
179
+ Java Code:
158
180
159
181
```java
160
182
public class Solution {
@@ -192,6 +214,8 @@ public class Solution {
192
214
}
193
215
```
194
216
217
+ Python3 Code:
218
+
195
219
``` py
196
220
class Solution :
197
221
def longestValidParentheses (self , s : str ) -> int :
@@ -219,6 +243,31 @@ class Solution:
219
243
return ans
220
244
```
221
245
246
+ CPP Code:
247
+
248
+ ``` cpp
249
+ class Solution {
250
+ public:
251
+ int longestValidParentheses(string s) {
252
+ int left = 0, right = 0, ans = 0, N = s.size();
253
+ for (int i = 0; i < N; ++i) {
254
+ left += s[ i] == '(';
255
+ right += s[ i] == ')';
256
+ if (left == right) ans = max(ans, left + right);
257
+ else if (right > left) left = right = 0;
258
+ }
259
+ left = 0, right = 0;
260
+ for (int i = N - 1; i >= 0; --i) {
261
+ left += s[ i] == '(';
262
+ right += s[ i] == ')';
263
+ if (left == right) ans = max(ans, left + right);
264
+ else if (left > right) left = right = 0;
265
+ }
266
+ return ans;
267
+ }
268
+ };
269
+ ```
270
+
222
271
## 动态规划
223
272
224
273
### 思路
@@ -245,7 +294,9 @@ s = '(())())'
245
294
246
295
### 代码
247
296
248
- Python Code:
297
+ 代码支持:Python3, CPP
298
+
299
+ Python3 Code:
249
300
250
301
``` py
251
302
class Solution :
@@ -273,6 +324,26 @@ class Solution:
273
324
return mlen
274
325
```
275
326
327
+ CPP Code:
328
+
329
+ ``` cpp
330
+ class Solution {
331
+ public:
332
+ int longestValidParentheses(string s) {
333
+ vector<int > dp(s.size() + 1, 0);
334
+ int ans = 0;
335
+ for (int i = 0; i < s.size(); ++i) {
336
+ if (s[ i] == '(') continue;
337
+ int start = i - dp[ i] - 1;
338
+ if (start >= 0 && s[ start] == '(')
339
+ dp[ i + 1] = dp[ i] + 2 + dp[ start] ;
340
+ ans = max(ans, dp[ i + 1] );
341
+ }
342
+ return ans;
343
+ }
344
+ };
345
+ ```
346
+
276
347
**复杂度分析**
277
348
278
349
- 时间复杂度:$$O(N)$$
@@ -292,7 +363,6 @@ class Solution:
292
363
1. 如果判断的不仅仅只有'('和')', 还有'[', ']', '{'和'}', 该怎么办?
293
364
2. 如果输出的不是长度, 而是任意一个最长有效括号对的字符串, 该怎么办?
294
365
295
-
296
366
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
297
367
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
298
368

0 commit comments