Skip to content

Commit 3dec2fd

Browse files
committed
Longest Valid Parentheses
1 parent 6fb19aa commit 3dec2fd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

solutions/032.Longest_Valid_Parentheses/AC_simulation_n.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Solution {
1515
int longestValidParentheses(string s) {
1616
int max_len = 0, depth = 0, start = -1;
1717

18-
// solve ()))
18+
// solve ((()
1919
for (int i = 0; i < s.size(); ++i) {
2020
if (s[i] == '(')
2121
++depth;
@@ -30,7 +30,7 @@ class Solution {
3030
}
3131
}
3232

33-
// solve ((()
33+
// solve ()))
3434
depth = 0;
3535
start = s.size();
3636
for (int i = s.size(); i >= 0; --i) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## 032. Longest Valid Parentheses (Hard)
2+
3+
### **链接**
4+
题目:https://oj.leetcode.com/problems/longest-valid-parentheses/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
问一个字符串里最长的合法括号串的长度。
9+
10+
### **分析**
11+
12+
1. **(C++)**用栈来做,如果匹配就出栈,然后长度就是 `cur - stack_top_pos` 也就是 - 匹配的前一个位置。 O(n) time, O(n) space。
13+
2. **(C++)**栈消耗空间太多了,其实可以维护 () 匹配的长度,不过可能出现 `()))``((()` 的情况,所以要前后各扫一遍。O(n) time, O(1) space。
14+
3. 用较复杂的 DP 来做,不过空间可没解法 2 那么优了。刚看到[我很久前的一个解法](http://blog.csdn.net/hcbbt/article/details/15494035),用太多空间了Orz。现在来看还是 1、2 的做法好。

0 commit comments

Comments
 (0)