码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode - Longest Valid Parentheses

时间:2014-10-26 11:49:43      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   for   sp   on   amp   

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.

class Solution {
public:
    int longestValidParentheses(std::string s) {
        int dp[1000005], i, max = 0;
		memset(dp, 0, sizeof(dp));
		for(i = 1; i < s.size(); i++) 
		{
			if(s[i] != ')' || s[i-dp[i]-1] != '(') continue;
			dp[i+1] = 2 + dp[i] + dp[i-dp[i]-1];
			if(dp[i+1] > max) max = dp[i+1];
		}
		return max;
    }
};


leetcode - Longest Valid Parentheses

标签:style   color   io   os   ar   for   sp   on   amp   

原文地址:http://blog.csdn.net/akibatakuya/article/details/40474931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!