Longest Valid Parentheses

My Submissions

Question Solution 
Total Accepted: 47520 Total Submissions: 222865 Difficulty: Hard

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.

#include<iostream>
#include<string>
using namespace std;
#define NUM 350 class Solution { public:
int longestValidParentheses(string s)
{
int len = s.length();
if (len < ) return ;
//int**dp= (int **)new int[10000][10000];
//int** isValid=(int **)new int[10000][10000];
//int max = 0;
//memset(dp, 0, 10000 * 10000 * sizeof(int));
//memset(isValid, 0, 10000 * 10000 * sizeof(int));
int dp[NUM][NUM];
int isValid[NUM][NUM];
int max = ;
memset(dp, , NUM * NUM * sizeof(int));//会影响到结果输出
memset(isValid, , NUM * NUM * sizeof(int));
for (int i = ; i < s.length(); ++i)
{
for (int j = i - ; j >= ; --j)
{
if (s[j] = '('&&s[i] == ')')//情况一
{
int temp = ;
for (int k = j + ; k < i; ++k)
{
if (isValid[j][k] && isValid[k + ][i])
temp = ;
}
if (i == j + || dp[j + ][i - ] || temp)
{
isValid[j][i] = ;
dp[j][i] = i - j + ;
max = max > dp[j][i] ? max : dp[j][i];
}
else
{
isValid[j][i] = ;
dp[j][i] = dp[j + ][i] > dp[j][i - ] ? dp[j + ][i] : dp[j][i - ];
}
}
else if (s[j] == '('&&s[i] == '(')//情况二
{
isValid[j][i] = ;
dp[j][i] = dp[j][i - ];
}
else if (s[j] == ')'&&s[i] == ')')//情况三
{
isValid[j][i] = ;
dp[j][i] = dp[j + ][i];
}
else//情况四
{
isValid[j][i] = ;
dp[j][i] = dp[j + ][i - ];
}
}
}
return max;
}
}; int main()
{
Solution test;
string s1 = ")(())()";
int res = test.longestValidParentheses(s1);
cout << res << endl;
return ;
}

无奈,只好搜索求助大神,dp:

这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length()的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1]最长的有效匹配括号子串长度。则存在如下关系:
dp[s.length - 1] = 0;从i - 2 到0逆向求dp[],并记录其最大值。
若s[i] == '(',则在s中从i开始到s.length - 1计算s[i]的值。这个计算分为两步,通过dp[i + 1]进行的(注意dp[i + 1]已经在上一步求解):
在s中寻找从i + 1开始的有效括号匹配子串长度,即dp[i + 1],跳过这段有效的括号子串,查看下一个字符,其下标为j = i + 1 + dp[i + 1]。若j没有越界,并且s[j] == ‘)’,则s[i ... j]为有效括号匹配,dp[i] =dp[i + 1] + 2。
在求得了s[i ... j]的有效匹配长度之后,若j + 1没有越界,则dp[i]的值还要加上从j + 1开始的最长有效匹配,即dp[j + 1]。

O(n)

 int longestValidParentheses(string s) {
// Note: The Solution object is instantiated only once.
int slen = s.length();
if(slen<)return ;
int max = ;
int* dp = new int[slen];
memset(dp,,sizeof(int)*slen); for(int i=slen-; i>=;i--)
{
if(s[i]=='(')
{
int j = i++dp[i+];
if(j<slen && s[j]==')')
{
dp[i]=dp[i+]+;
int k = ;
if(j+<slen)k=dp[j+];
dp[i] += k;
}
max = max>dp[i]?max:dp[i];
}
}
delete[] dp;
return max;
}

自己的理解大神思想精髓并用对称顺序实现:

 class Solution {
public:
int longestValidParentheses(string s) {
int max=;
int len=s.size();
int *dp=new int[len];//dp[i]表示从s[0]到s[i-1]最长的字符有效匹配长度
for(int i=;i<len;i++)
dp[i]=;
for(int i=;i<s.size();i++)
{
if(s[i]==')')
{
int j=i-dp[i-]-;
if(j>=&&s[j]=='(')
{
dp[i]=dp[i-]+;
int k=;
if(j->=)
k=dp[j-];
dp[i]+=k;
}
max=dp[i]>max?dp[i]:max;
}
}
delete[] dp;
return max;
}
};

这段代码当真高明啊!!

Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  6. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

随机推荐

  1. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

  2. 含大量行的订单创建时候creditlimit校验最耗时间

    含大量行的订单创建时候creditlimit校验最耗时间

  3. mac安装chromedriver报错

    运行提示:Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/c ...

  4. saybyeto2015

    不知不觉一年又结束了,还是打算在最后一天写个总结. 今年换了工作,改变不可谓不多. 技术方面,看的书主要是 <Angularjs权威指南> <JavaScript设计模式与开发实践& ...

  5. 一个最小化的SpringBoot项目

    项目结构 项目基于Maven管理,注意使用了父pom <parent> <groupId>org.springframework.boot</groupId> &l ...

  6. 码农谷 找出N之内的所有完数

    题目描述 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是"完数". 编程序找出N之内的所有完数, ...

  7. [cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)

    [转]http://blog.csdn.net/realcrazysun1/article/details/42393629 本文基于cocos2d-js 3.0版本引擎开发 RenderTextur ...

  8. socket笔记

    参考: http://www.cnblogs.com/dolphinX/p/3460545.html http://www.cnblogs.com/wei2yi/archive/2011/03/23/ ...

  9. gcc相关

    linux操作系统上面开发程序, 光有了gcc 是不行的 它还需要一个   build-essential软件包作用是提供编译程序必须软件包的列表信息 也就是说 编译程序有了这个软件包它才知道 头文件 ...

  10. 关于 NPOI 报 Invalid column index (256). Allowable column range for BIFF8 is (0..255) or ('A'..'IV') 错误的解决办法

    当看到这个错误的时候,网上搜索可以会有些说列数有限制之类的说法,这个说法是相对于 Office 2003 的,在 Office 2007 之前,最多只可以创建  列:在 Office 2007 之后, ...