Skip to content

Commit 819f209

Browse files
committed
feat: add solutions to lc problem: No.0032
No.0032.Longest Valid Parentheses
1 parent 3028706 commit 819f209

File tree

6 files changed

+314
-45
lines changed

6 files changed

+314
-45
lines changed

solution/0000-0099/0032.Longest Valid Parentheses/README.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,142 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:动态规划**
54+
55+
定义 `dp[i]` 表示以 `s[i]` 结尾的最长有效括号的长度,答案为 `max(dp[i])`
56+
57+
`dp[i]` 的值有以下几种情况:
58+
59+
-`s[i]``(`,那么 `dp[i] = 0`
60+
-`s[i]``)`,且 `s[i - 1]``(`,那么 `dp[i] = dp[i - 2] + 2`
61+
-`s[i]``)`,且 `s[i - 1]``)``s[i - dp[i - 1] - 1]``(`,那么 `dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]`
62+
63+
以上需要注意边界的判断处理。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `s` 的长度。
66+
5367
<!-- tabs:start -->
5468

5569
### **Python3**
5670

5771
<!-- 这里可写当前语言的特殊实现逻辑 -->
5872

5973
```python
60-
74+
class Solution:
75+
def longestValidParentheses(self, s: str) -> int:
76+
n = len(s)
77+
if n < 2:
78+
return 0
79+
dp = [0] * n
80+
for i in range(1, n):
81+
if s[i] == ')':
82+
if s[i - 1] == '(':
83+
dp[i] = 2 + (dp[i - 2] if i > 1 else 0)
84+
else:
85+
j = i - dp[i - 1] - 1
86+
if j >= 0 and s[j] == '(':
87+
dp[i] = 2 + dp[i - 1] + (dp[j - 1] if j else 0)
88+
return max(dp)
6189
```
6290

6391
### **Java**
6492

6593
<!-- 这里可写当前语言的特殊实现逻辑 -->
6694

6795
```java
96+
class Solution {
97+
public int longestValidParentheses(String s) {
98+
int n = s.length();
99+
if (n < 2) {
100+
return 0;
101+
}
102+
char[] cs = s.toCharArray();
103+
int[] dp = new int[n];
104+
int ans = 0;
105+
for (int i = 1; i < n; ++i) {
106+
if (cs[i] == ')') {
107+
if (cs[i - 1] == '(') {
108+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
109+
} else {
110+
int j = i - dp[i - 1] - 1;
111+
if (j >= 0 && cs[j] == '(') {
112+
dp[i] = 2 + dp[i - 1] + (j > 0 ? dp[j - 1] : 0);
113+
}
114+
}
115+
ans = Math.max(ans, dp[i]);
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int longestValidParentheses(string s) {
129+
int n = s.size();
130+
if (n < 2) return 0;
131+
vector<int> dp(n);
132+
int ans = 0;
133+
for (int i = 1; i < n; ++i) {
134+
if (s[i] == ')') {
135+
if (s[i - 1] == '(') {
136+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
137+
} else {
138+
int j = i - dp[i - 1] - 1;
139+
if (~j && s[j] == '(') {
140+
dp[i] = 2 + dp[i - 1] + (j ? dp[j - 1] : 0);
141+
}
142+
}
143+
ans = max(ans, dp[i]);
144+
}
145+
}
146+
return ans;
147+
}
148+
};
149+
```
68150
151+
### **Go**
152+
153+
```go
154+
func longestValidParentheses(s string) int {
155+
n := len(s)
156+
if n < 2 {
157+
return 0
158+
}
159+
dp := make([]int, n)
160+
ans := 0
161+
for i := 1; i < n; i++ {
162+
if s[i] == ')' {
163+
if s[i-1] == '(' {
164+
dp[i] = 2
165+
if i > 1 {
166+
dp[i] += dp[i-2]
167+
}
168+
} else {
169+
j := i - dp[i-1] - 1
170+
if j >= 0 && s[j] == '(' {
171+
dp[i] = 2 + dp[i-1]
172+
if j > 0 {
173+
dp[i] += dp[j-1]
174+
}
175+
}
176+
}
177+
ans = max(ans, dp[i])
178+
}
179+
}
180+
return ans
181+
}
182+
183+
func max(a, b int) int {
184+
if a > b {
185+
return a
186+
}
187+
return b
188+
}
69189
```
70190

71191
### **...**

solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,119 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def longestValidParentheses(self, s: str) -> int:
50+
n = len(s)
51+
if n < 2:
52+
return 0
53+
dp = [0] * n
54+
for i in range(1, n):
55+
if s[i] == ')':
56+
if s[i - 1] == '(':
57+
dp[i] = 2 + (dp[i - 2] if i > 1 else 0)
58+
else:
59+
j = i - dp[i - 1] - 1
60+
if j >= 0 and s[j] == '(':
61+
dp[i] = 2 + dp[i - 1] + (dp[j - 1] if j else 0)
62+
return max(dp)
4963
```
5064

5165
### **Java**
5266

5367
```java
68+
class Solution {
69+
public int longestValidParentheses(String s) {
70+
int n = s.length();
71+
if (n < 2) {
72+
return 0;
73+
}
74+
char[] cs = s.toCharArray();
75+
int[] dp = new int[n];
76+
int ans = 0;
77+
for (int i = 1; i < n; ++i) {
78+
if (cs[i] == ')') {
79+
if (cs[i - 1] == '(') {
80+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
81+
} else {
82+
int j = i - dp[i - 1] - 1;
83+
if (j >= 0 && cs[j] == '(') {
84+
dp[i] = 2 + dp[i - 1] + (j > 0 ? dp[j - 1] : 0);
85+
}
86+
}
87+
ans = Math.max(ans, dp[i]);
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int longestValidParentheses(string s) {
101+
int n = s.size();
102+
if (n < 2) return 0;
103+
vector<int> dp(n);
104+
int ans = 0;
105+
for (int i = 1; i < n; ++i) {
106+
if (s[i] == ')') {
107+
if (s[i - 1] == '(') {
108+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
109+
} else {
110+
int j = i - dp[i - 1] - 1;
111+
if (~j && s[j] == '(') {
112+
dp[i] = 2 + dp[i - 1] + (j ? dp[j - 1] : 0);
113+
}
114+
}
115+
ans = max(ans, dp[i]);
116+
}
117+
}
118+
return ans;
119+
}
120+
};
121+
```
54122
123+
### **Go**
124+
125+
```go
126+
func longestValidParentheses(s string) int {
127+
n := len(s)
128+
if n < 2 {
129+
return 0
130+
}
131+
dp := make([]int, n)
132+
ans := 0
133+
for i := 1; i < n; i++ {
134+
if s[i] == ')' {
135+
if s[i-1] == '(' {
136+
dp[i] = 2
137+
if i > 1 {
138+
dp[i] += dp[i-2]
139+
}
140+
} else {
141+
j := i - dp[i-1] - 1
142+
if j >= 0 && s[j] == '(' {
143+
dp[i] = 2 + dp[i-1]
144+
if j > 0 {
145+
dp[i] += dp[j-1]
146+
}
147+
}
148+
}
149+
ans = max(ans, dp[i])
150+
}
151+
}
152+
return ans
153+
}
154+
155+
func max(a, b int) int {
156+
if a > b {
157+
return a
158+
}
159+
return b
160+
}
55161
```
56162

57163
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int longestValidParentheses(string s) {
4+
int n = s.size();
5+
if (n < 2) return 0;
6+
vector<int> dp(n);
7+
int ans = 0;
8+
for (int i = 1; i < n; ++i) {
9+
if (s[i] == ')') {
10+
if (s[i - 1] == '(') {
11+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
12+
} else {
13+
int j = i - dp[i - 1] - 1;
14+
if (~j && s[j] == '(') {
15+
dp[i] = 2 + dp[i - 1] + (j ? dp[j - 1] : 0);
16+
}
17+
}
18+
ans = max(ans, dp[i]);
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
func longestValidParentheses(s string) int {
2+
n := len(s)
3+
if n < 2 {
4+
return 0
5+
}
6+
dp := make([]int, n)
7+
ans := 0
8+
for i := 1; i < n; i++ {
9+
if s[i] == ')' {
10+
if s[i-1] == '(' {
11+
dp[i] = 2
12+
if i > 1 {
13+
dp[i] += dp[i-2]
14+
}
15+
} else {
16+
j := i - dp[i-1] - 1
17+
if j >= 0 && s[j] == '(' {
18+
dp[i] = 2 + dp[i-1]
19+
if j > 0 {
20+
dp[i] += dp[j-1]
21+
}
22+
}
23+
}
24+
ans = max(ans, dp[i])
25+
}
26+
}
27+
return ans
28+
}
29+
30+
func max(a, b int) int {
31+
if a > b {
32+
return a
33+
}
34+
return b
35+
}
Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
class Solution {
22
public int longestValidParentheses(String s) {
3-
if (s == null || s.length() < 2) {
3+
int n = s.length();
4+
if (n < 2) {
45
return 0;
56
}
6-
char[] chars = s.toCharArray();
7-
int n = chars.length;
8-
int[] res = new int[n];
9-
res[0] = 0;
10-
res[1] = chars[1] == ')' && chars[0] == '(' ? 2 : 0;
11-
12-
int max = res[1];
13-
14-
for (int i = 2; i < n; ++i) {
15-
if (chars[i] == ')') {
16-
if (chars[i - 1] == '(') {
17-
res[i] = res[i - 2] + 2;
7+
char[] cs = s.toCharArray();
8+
int[] dp = new int[n];
9+
int ans = 0;
10+
for (int i = 1; i < n; ++i) {
11+
if (cs[i] == ')') {
12+
if (cs[i - 1] == '(') {
13+
dp[i] = 2 + (i > 1 ? dp[i - 2] : 0);
1814
} else {
19-
int index = i - res[i - 1] - 1;
20-
if (index >= 0 && chars[index] == '(') {
21-
// ()(())
22-
res[i] = res[i - 1] + 2 + (index - 1 >= 0 ? res[index - 1] : 0);
15+
int j = i - dp[i - 1] - 1;
16+
if (j >= 0 && cs[j] == '(') {
17+
dp[i] = 2 + dp[i - 1] + (j > 0 ? dp[j - 1] : 0);
2318
}
2419
}
20+
ans = Math.max(ans, dp[i]);
2521
}
26-
max = Math.max(max, res[i]);
2722
}
28-
29-
return max;
23+
return ans;
3024
}
3125
}

0 commit comments

Comments
 (0)