[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; } };
相关文章推荐
- LeetCode | 32. Longest Valid Parentheses——最长有效括号匹配长度
- LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- 【LeetCode】最长括号匹配Longest Valid Parentheses
- Longest Valid Parentheses(最长的括号匹配)【面试算法leetcode】
- LeetCode-Longest Valid Parentheses-最长匹配括号-栈的应用
- LeetCode | Longest Valid Parentheses(最长有效的括号匹配)
- 最长括号匹配长度
- 栈应用——最长括号匹配
- LeetCodet题解--22. Generate Parentheses(生成n对匹配的括号)
- Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- 顺序栈(进制转换 + 括号匹配 + 判断栈表 + 最长括号匹配长度)
- 32.最长的合法括号匹配
- leetcode 20 简单括号匹配
- 算法学习-最长括号匹配
- leetcode:括号匹配问题
- Longest Valid Parentheses:最长括号子段匹配
- leetcode括号匹配问题汇总
- leetcode 括号匹配
- LeetCode 32. Longest Valid Parentheses(最长有效括号)
- [算法基础练习]最长括号匹配