给你一个只包含 ‘(’ 和 ‘)’ 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
示例 1:
输入:s = “(()”
输出:2
解释:最长有效括号子串是 “()”
示例 2:
输入:s = “)()())”
输出:4
解释:最长有效括号子串是 “()()”
示例 3:
输入:s = “”
输出:0
提示:
0 <= s.length <= 3 * 104
s[i] 为 ‘(’ 或 ‘)’
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-valid-parentheses
class Solution {
/*
dp[i]:一定有i个字符,最长的有效长度
最后一步:
i个字符为'(',那么肯定没有')'跟其匹配,dp[i]=0
i个字符为')':
判断i-1个字符为什么,分情况讨论
i-1为'(',那么dp[i] = 2+dp[i-2];
//i和i-1构成一个()+dp[i-2]包含i-2位置字符的最长有效长度
i-1为')',那么又要分情况讨论
去判断与i-1位置')'匹配的'('位置的前一个:i-1-dp[i-1]
i-1-dp[i-1]位置为:'(',那么就是dp[i] = dp[i-1]+2
i-1-dp[i-1]位置为:')',那么dp[i]=0
*/
public int longestValidParentheses(String s) {
if(s==null||s.length()==0){
return 0;
}
char[] chars = s.toCharArray();
int[] dp = new int[chars.length];
dp[0]=0;
int res = 0;
for(int i=1;i<chars.length;i++){
if(chars[i]=='('){
dp[i] = 0;
}else{ //那么当前字符为')'
if(chars[i-1]=='('){
dp[i] = 2;
dp[i]+=((i-2)>=0?dp[i-2]:0);
}else{ //i-1个位置的字符为')',去找与其匹配的'('的上一个位置
int index = i-1-dp[i-1];
if(index>=0&&chars[index]=='('){
dp[i] = dp[i-1]+2;
if(index-1>=0&&chars[index-1]==')'){
dp[i]+=dp[index-1];
}
}else{ //chars[index]==')'
dp[i] = 0;
}
}
}
res = Math.max(res,dp[i]);
}
return res;
}
}