您的位置:首页 > 其它

[LeetCode]—Longest Valid Parentheses 最长括号匹配

2014-07-07 19:20 363 查看


Longest Valid Parentheses

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

For
"(()"
, the longest valid parentheses substring is
"()"
,
which has length = 2.

Another example is
")()())"
, where the longest valid parentheses substring
is
"()()"
, which has length = 4.
分析:
由于是计算长度,直接单纯栈并不能解决问题。但是如果可以用栈记录下标,那么还是可以求解的。

方法一:用栈存储“)”的下标,当遇到“(”的时候进行分析,时间复杂度O(N):

从前之后,遍历string。遇到“(”入栈,遇到“)”讨论。其中last记录最后不匹配的下标,初始化为-1。

1、此时栈为空,那说明“)”不能匹配。用last记录下其坐标。

2、当栈不为空时,匹配上,出栈后:(1)此时栈依然不空,那么当前可匹配的括号为i-st.top();

(2)此时栈空了,说明之前全部匹配上。那么最后一个没匹配的存在last中。i-last就是之前匹配上的括号的数目。

代码出自:https://gitcafe.com/soulmachine/LeetCode

class Solution{
public:
int longestValidParentheses(string s){
stack<int>  st;  //存的是‘(’的下标
int max_len=0,last=-1;  //last 存的是最后一次没匹配的')'

for(int i=0;i<s.size();i++){
if(s[i]=='('){
st.push(i);
}else{  //重点考察“)”,以找到以第一个未被匹配的“(”为参考。
if(st.empty()){
last=i;
}
else {
st.pop();
if(st.empty())
max_len=max(max_len,i-last);
else{
max_len=max(max_len,i-st.top());
}
}
}
}
return max_len;

}

};


方法二:用一维动态规划逆向求解。dp[i]表示从s[i]到s[s.length - 1]包含s[i]的最长的有效匹配括号子串长度。

方法出自:http://blog.csdn.net/yapian8/article/details/28239003

class Solution {
public:
int longestValidParentheses(string s) {
int n=s.length();
if(n<2)return 0;
int *dp=new int
;
int max_len=0;
for(int i=0;i<n;i++)
dp[i]=0;
for(int i=n-2;i>=0;i--){
if(s[i]=='('){   //只处理“(”,右括号设为0
int j=i+1+dp[i+1];
if(s[j]==')'&& j<n){
dp[i]=dp[i+1]+2;
if(j+1<n)
dp[i]+=dp[j+1];
}
}
max_len=max(max_len,dp[i]);
}
return max_len;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: