32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"


 1 class Solution {
 2     
 3     public int longestValidParentheses(String s) {
 4         int n = s.length();
 5         int []f = new int[n];
 6         int []stack = new int[n];
 7         int ans = 0;
 8         int k = 0;
 9         for (int i = 0; i < n; ++i) {
10             if (s.charAt(i) == '(') {
11                 stack[k++] = i;
12             }
13             if (s.charAt(i) == ')') {
14                 if (k > 0) {
15                     f[i] = i - stack[k - 1] + 1;
16                     if (stack[k - 1] - 1 >= 0 && f[stack[k - 1] - 1] > 0)
17                         f[i] += f[stack[k - 1] - 1];
18                     k--;
19                 } else {
20                     k = 0;
21                     
22                 }
23                 
24             }
25         }
26         for (int i = 0; i < n; ++i) {
27             ans = Math.max(ans, f[i]);
28         }
29         
30         return ans;
31     }
32 }

 

 1 public class Solution {
 2 
 3     public int longestValidParentheses(String s) {
 4         int n = s.length();
 5         int []dp = new int[n + 1];
 6         for (int i = 0; i < n; ++i) {
 7             if (s.charAt(i) == ')') {
 8                 if (i - 1 >= 0 && s.charAt(i - 1) == '(') {
 9                     
10                     dp[i + 1] = 2 + dp[i - 2 + 1];
11                     
12                 }
13                 if ( i - 1 - dp[i] >= 0 && s.charAt(i - 1 - dp[i]) == '(') {
14                     dp[i + 1] = Math.max(dp[i + 1], 2 + dp[i] + dp[i - 1 - dp[i]] );
15                 }
16                 
17             }
18         }
19         int ans = 0;
20         for (int i = 1; i <= n; ++i) ans = Math.max(ans, dp[i]);
21         //for (int i = 1; i <= n; ++i) System.out.print(dp[i] + " ");
22         return ans;
23     }
24 }
 1 public class Solution {
 2 
 3     public int longestValidParentheses(String s) {
 4         int n = s.length();
 5         int left = 0, right = 0;
 6         int ans = 0;
 7         for (int i = 0; i < n; ++i) {
 8             if (s.charAt(i) == '(') left++;
 9             if (s.charAt(i) == ')') right++;
10             if (left == right) {
11                 ans = Math.max(ans, 2*left);
12             } else if(left < right) {
13                 left = 0;
14                 right = 0;
15                 
16             }
17         }
18         left = right = 0;
19         for (int i = n - 1; i >= 0; --i) {
20             if (s.charAt(i) == '(') left++;
21             if (s.charAt(i) == ')') right++;
22             if (left == right) {
23                 ans = Math.max(ans, 2*left);
24             } else if(left > right) {
25                 left = 0;
26                 right = 0;
27                 
28             }
29         }
30         return ans;
31     }
32 }

 

posted @ 2020-02-14 20:42  hyx1  阅读(98)  评论(0编辑  收藏  举报