File tree 2 files changed +16
-2
lines changed
solutions/032.Longest_Valid_Parentheses
2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class Solution {
15
15
int longestValidParentheses (string s) {
16
16
int max_len = 0 , depth = 0 , start = -1 ;
17
17
18
- // solve ()) )
18
+ // solve ((( )
19
19
for (int i = 0 ; i < s.size (); ++i) {
20
20
if (s[i] == ' (' )
21
21
++depth;
@@ -30,7 +30,7 @@ class Solution {
30
30
}
31
31
}
32
32
33
- // solve ((( )
33
+ // solve ()) )
34
34
depth = 0 ;
35
35
start = s.size ();
36
36
for (int i = s.size (); i >= 0 ; --i) {
Original file line number Diff line number Diff line change
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 的做法好。
You can’t perform that action at this time.
0 commit comments