Skip to content

Commit b0d162a

Browse files
committed
Add solution 032
1 parent f0b41b4 commit b0d162a

File tree

3 files changed

+138
-39
lines changed

3 files changed

+138
-39
lines changed

README.md

+43-39
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<p align="center">
2-
<a href="https://github.com/yanglbme/leetcode"><img src="http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png" alt="LeetCode-GitHub"></a>
2+
<a href="https://github.com/doocs/leetcode"><img src="http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png" alt="LeetCode-GitHub"></a>
33
</p>
44

55
<p align="center">
6+
<a href="https://github.com/doocs/leetcode"><img src="https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg" alt="Language"></a>
67
<a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-Welcome-brightgreen.svg" alt="PRs Welcome"></a>
7-
<a href="https://github.com/yanglbme/leetcode"><img src="https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg" alt="Language"></a>
8+
<a href="https://github.com/doocs/leetcode/issues"><img src="https://img.shields.io/github/issues/doocs/leetcode.svg" alt="issues"></a>
9+
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://img.shields.io/github/stars/doocs/leetcode.svg" alt="stars"></a>
10+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
811
</p>
912

1013
## Introduction
@@ -16,54 +19,55 @@ Complete solutions to Leetcode problems, updated daily.
1619

1720
| # | Title | Tags |
1821
|---|---|---|
19-
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
20-
| 007 | [Reverse Integer](https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
21-
| 013 | [Roman to Integer](https://github.com/yanglbme/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
22-
| 014 | [Longest Common Prefix](https://github.com/yanglbme/leetcode/tree/master/solution/014.Longest%20Common%20Prefix) | `String` |
23-
| 020 | [Valid Parentheses](https://github.com/yanglbme/leetcode/tree/master/solution/020.Valid%20Parentheses) | `String`, `Stack` |
24-
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
25-
| 053 | [Maximum Subarray](https://github.com/yanglbme/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
26-
| 070 | [Climbing Stairs](https://github.com/yanglbme/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
27-
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
28-
| 136 | [Single Number](https://github.com/yanglbme/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
29-
| 189 | [Rotate Array](https://github.com/yanglbme/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
30-
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
31-
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
32-
| 231 | [Power of Two](https://github.com/yanglbme/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
33-
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/yanglbme/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
34-
| 237 | [Delete Node in a Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
35-
| 344 | [Reverse String](https://github.com/yanglbme/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
36-
| 876 | [Middle of the Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
22+
| 001 | [Two Sum](https://github.com/doocs/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
23+
| 007 | [Reverse Integer](https://github.com/doocs/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
24+
| 013 | [Roman to Integer](https://github.com/doocs/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
25+
| 014 | [Longest Common Prefix](https://github.com/doocs/leetcode/tree/master/solution/014.Longest%20Common%20Prefix) | `String` |
26+
| 020 | [Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/020.Valid%20Parentheses) | `String`, `Stack` |
27+
| 021 | [Merge Two Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
28+
| 053 | [Maximum Subarray](https://github.com/doocs/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
29+
| 070 | [Climbing Stairs](https://github.com/doocs/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
30+
| 083 | [Remove Duplicates from Sorted List](https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
31+
| 136 | [Single Number](https://github.com/doocs/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
32+
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
33+
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
34+
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
35+
| 231 | [Power of Two](https://github.com/doocs/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
36+
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
37+
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
38+
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
39+
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
3740

3841

3942
### Medium
4043

4144
| # | Title | Tags |
4245
|---|---|---|
43-
| 002 | [Add Two Numbers](https://github.com/yanglbme/leetcode/tree/master/solution/002.Add%20Two%20Numbers) | `Linked List`, `Math` |
44-
| 015 | [3Sum](https://github.com/yanglbme/leetcode/tree/master/solution/015.3Sum) | `Array`, `Two Pointers` |
45-
| 019 | [Remove Nth Node From End of List](https://github.com/yanglbme/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
46-
| 024 | [Swap Nodes in Pairs](https://github.com/yanglbme/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
47-
| 031 | [Next Permutation](https://github.com/yanglbme/leetcode/tree/master/solution/031.Next%20Permutation) | `Array` |
48-
| 046 | [Permutations](https://github.com/yanglbme/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
49-
| 047 | [Permutations II](https://github.com/yanglbme/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
50-
| 062 | [Unique Paths](https://github.com/yanglbme/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
51-
| 063 | [Unique Paths II](https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
52-
| 075 | [Sort Colors](https://github.com/yanglbme/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
53-
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
54-
| 086 | [Partition List](https://github.com/yanglbme/leetcode/tree/master/solution/086.Partition%20List) | `Linked List`, `Two Pointers` |
55-
| 092 | [Reverse Linked List II](https://github.com/yanglbme/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II) | `Linked List` |
56-
| 094 | [Binary Tree Inorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
57-
| 144 | [Binary Tree Preorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
58-
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/yanglbme/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
46+
| 002 | [Add Two Numbers](https://github.com/doocs/leetcode/tree/master/solution/002.Add%20Two%20Numbers) | `Linked List`, `Math` |
47+
| 015 | [3Sum](https://github.com/doocs/leetcode/tree/master/solution/015.3Sum) | `Array`, `Two Pointers` |
48+
| 019 | [Remove Nth Node From End of List](https://github.com/doocs/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
49+
| 024 | [Swap Nodes in Pairs](https://github.com/doocs/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
50+
| 031 | [Next Permutation](https://github.com/doocs/leetcode/tree/master/solution/031.Next%20Permutation) | `Array` |
51+
| 046 | [Permutations](https://github.com/doocs/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
52+
| 047 | [Permutations II](https://github.com/doocs/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
53+
| 062 | [Unique Paths](https://github.com/doocs/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
54+
| 063 | [Unique Paths II](https://github.com/doocs/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
55+
| 075 | [Sort Colors](https://github.com/doocs/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
56+
| 082 | [Remove Duplicates from Sorted List II](https://github.com/doocs/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
57+
| 086 | [Partition List](https://github.com/doocs/leetcode/tree/master/solution/086.Partition%20List) | `Linked List`, `Two Pointers` |
58+
| 092 | [Reverse Linked List II](https://github.com/doocs/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II) | `Linked List` |
59+
| 094 | [Binary Tree Inorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
60+
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
61+
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
5962

6063

6164
### Hard
6265

6366
| # | Title | Tags |
6467
|---|---|---|
65-
| 023 | [Merge k Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/023.Merge%20k%20Sorted%20Lists) | `Linked List`, `Divide and Conquer`, `Heap` |
66-
| 145 | [Binary Tree Postorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
68+
| 023 | [Merge k Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/023.Merge%20k%20Sorted%20Lists) | `Linked List`, `Divide and Conquer`, `Heap` |
69+
| 032 | [Longest Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/032.Longest%20Valid%20Parentheses) | `String`, `Dynamic Programming` |
70+
| 145 | [Binary Tree Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
6771

6872
## Contributions
6973
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
@@ -73,7 +77,7 @@ I'm looking for long-term contributors/partners to this repo! Send me PRs if you
7377
## Contributors
7478

7579
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
76-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=chakyam "Code") </center> |
80+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>doocs</b></sub>](https://github.com/doocs)<br />[💻](https://github.com/doocs/leetcode/commits?author=doocs "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> |
7781
|---|---|
7882

7983
<!-- ALL-CONTRIBUTORS-LIST:END -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## 最长有效括号
2+
### 题目描述
3+
4+
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。
5+
6+
示例 1:
7+
```
8+
输入: "(()"
9+
输出: 2
10+
解释: 最长有效括号子串为 "()"
11+
```
12+
13+
示例 2:
14+
```
15+
输入: ")()())"
16+
输出: 4
17+
解释: 最长有效括号子串为 "()()"
18+
```
19+
20+
### 解法
21+
22+
此题采用动态规划方法。开辟一个数组空间 res,res[i] 表示必须以 s[i] 结尾的字符串的最长有效括号长度。
23+
24+
- 若 s[i] == '(',res[i] = 0;
25+
- 若 s[i] == ')' && s[i - 1] == '(',res[i] = res[i - 2] + 2;
26+
- 若 s[i] == ')' && s[i - 1] == ')',判断 s[i - 1 - res[i - 1]] 的符号,若为 '(',则 res[i] = res[i - 1] + 2 + res[i - res[i - 1] - 2]
27+
28+
注意数组下标越界检查。
29+
30+
```java
31+
class Solution {
32+
public int longestValidParentheses(String s) {
33+
if (s == null || s.length() < 2) {
34+
return 0;
35+
}
36+
char[] chars = s.toCharArray();
37+
int n = chars.length;
38+
int[] res = new int[n];
39+
res[0] = 0;
40+
res[1] = chars[1] == ')' && chars[0] == '(' ? 2 : 0;
41+
42+
int max = res[1];
43+
44+
for (int i = 2; i < n; ++i) {
45+
if (chars[i] == ')') {
46+
if (chars[i - 1] == '(') {
47+
res[i] = res[i - 2] + 2;
48+
} else {
49+
int index = i - res[i - 1] - 1;
50+
if (index >= 0 && chars[index] == '(') {
51+
// ()(())
52+
res[i] = res[i - 1] + 2 + (index - 1 >= 0 ? res[index - 1] : 0);
53+
}
54+
}
55+
}
56+
max = Math.max(max, res[i]);
57+
}
58+
59+
return max;
60+
61+
}
62+
}
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int longestValidParentheses(String s) {
3+
if (s == null || s.length() < 2) {
4+
return 0;
5+
}
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;
18+
} 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);
23+
}
24+
}
25+
}
26+
max = Math.max(max, res[i]);
27+
}
28+
29+
return max;
30+
31+
}
32+
}

0 commit comments

Comments
 (0)