[LeetCode] Longest Valid Parentheses
2015-06-13 15:29
405 查看
This problem is a nice extension of the Valid Parentheses problem.
There are several ways to solve it. The first idea is also to use a stack. However, in this time, we push the index instead of the character itself into the stack. What indexes do we push? Well, we push the indexes which seperate two valid parentheses.
Let's see an example "()(()" first. In this example, it is clear that the first two are valid and the last two are also valid. Thus the third characters seperate two valid parentheses. We push its index 3 into the stack. Then the longest valid parentheses is either on the left side of it or on the right side of it. For strings that have more than one such indexes, like "())())()", we push all of them into the stack. Then we visit each valid parentheses separated by them and find the longest length.
How do we obtain these indexes? In fact, you can simply follow the way in Valid Parentheses. Each time a mismatch occurrs, the current index is what we need.
You may run the following code on the above examples to get an understanding of how it works.
Of course, this problem also has a Dynamic Programming solution. Let's define P[i] to be the length of the longest valid parentheses end at i. Then state equations are:
Putting these together, we have the following code.
You may even notice that case 3 above has already includede case 2. So the code can be further shorten to below.
There are several ways to solve it. The first idea is also to use a stack. However, in this time, we push the index instead of the character itself into the stack. What indexes do we push? Well, we push the indexes which seperate two valid parentheses.
Let's see an example "()(()" first. In this example, it is clear that the first two are valid and the last two are also valid. Thus the third characters seperate two valid parentheses. We push its index 3 into the stack. Then the longest valid parentheses is either on the left side of it or on the right side of it. For strings that have more than one such indexes, like "())())()", we push all of them into the stack. Then we visit each valid parentheses separated by them and find the longest length.
How do we obtain these indexes? In fact, you can simply follow the way in Valid Parentheses. Each time a mismatch occurrs, the current index is what we need.
You may run the following code on the above examples to get an understanding of how it works.
class Solution { public: int longestValidParentheses (string s) { stack<int> indexes; for (int i = 0; i < (int)s.length(); i++) { if (s[i] == '('|| indexes.empty() || s[indexes.top()] != '(') indexes.push(i); else indexes.pop(); } if (indexes.empty()) return s.length(); int maxlen = 0, left = 0, right = s.length() - 1; while (!indexes.empty()) { left = indexes.top(); indexes.pop(); maxlen = max(maxlen, right - left); right = left - 1; } maxlen = max(maxlen, left); return maxlen; } };
Of course, this problem also has a Dynamic Programming solution. Let's define P[i] to be the length of the longest valid parentheses end at i. Then state equations are:
P[i] = 0if
s[i] == '('(No valid parentheses end with '(');
P[i] = P[i - 2] + 2if
s[i] == ')'and
s[i - 1] == '(', for example, s = '()()';
P[i] = P[i - P[i - 1] - 2] + P[i - 1] + 2if
s[i] == ')'and
s[i - 1] == ')'and
s[i - P[i - 1] - 1] == '(', for example, s = '(())'.
Putting these together, we have the following code.
class Solution { public: int longestValidParentheses(string s) { vector<int> dp(s.length(), 0); int maxlen = 0; for (int i = 1; i < (int)s.length(); i++) { if (s[i] == ')') { if (s[i - 1] == '(') dp[i] = 2 + (i - 2 >= 0 ? dp[i - 2] : 0); else if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0); maxlen = max(maxlen, dp[i]); } } return maxlen; } };
You may even notice that case 3 above has already includede case 2. So the code can be further shorten to below.
class Solution { public: int longestValidParentheses(string s) { vector<int> dp(s.length(), 0); int maxlen = 0; for (int i = 1; i < (int)s.length(); i++) { if (s[i] == ')' && i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') { dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0); maxlen = max(maxlen, dp[i]); } } return maxlen; } };
相关文章推荐
- Android:View事件分发机制
- cocos2dx3.2 ——屏幕适配
- 第一篇博客 对安卓的认识
- BZOJ 3626: [LNOI2014]LCA( 树链剖分 + 离线 )
- .NET Memory Allocation Profiling with Visual Studio 2012
- 安装VMware vCenter过程设置数据库方法
- Javascript中arguments用法实例分析
- #leetcode#Implement Stack using Queues
- 一个简单的c#逆向
- hdu 1009 贪心
- Java关键字final
- 【转载】 删除Win10“这台电脑”中的6个文件夹
- smart_stl:list小结
- Itunes 恢复时,卡在正在访问iTunes Store 停止不前的处理方法
- 快速排序
- Codeforces Round #306 (Div. 2) C.Divisibility by Eight
- Hadoop学习全程记录——在Eclipse中运行第一个MapReduce程序
- 我的博客之路
- 第5章源码
- UITransitionView and UILayoutContainerView