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. 类似新浪 腾讯微博字数统计 控制js(区分中英文 符号)

    <script> ; function Q(s) { return document.getElementById(s); } function checkWord(c) { len = ...

  2. Android软件更新安装。

    app的开发有一个问题是避免不了的,那就是软件的升级维护. 这里我在查过一些资料和写了一个升级帮助类.使用很方便.直接导入就可以了. ( VersionBean.class为更新地址返回的数据对象,我 ...

  3. Spark Streaming源码解读之流数据不断接收全生命周期彻底研究和思考

    本期内容 : 数据接收架构设计模式 数据接收源码彻底研究 一.Spark Streaming数据接收设计模式   Spark Streaming接收数据也相似MVC架构: 1. Mode相当于Rece ...

  4. jQuery最佳编程实践

    加载jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地址. <script t ...

  5. WinServer2008r2 机器时间格式修改

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法.windows2008默认时间格式 ...

  6. 使用 Git 管理源代码

    在现代软件开发项目中,要成为一个有效的软件开发人员,我们必须能够与其他项目贡献者并行进行开发.源代码管理(SCM)系统不是什么新思想.为了编写一些能够更快速.简单地开发以后软件项目的软件,已经进行了很 ...

  7. findstr 命令

    body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...

  8. winform 控件(2)

    1.picturebox:图片(属性)sizemide:调整图片 [出现在form的下方]2.imagelist--图片集(在form下方显示)有索引号,记住索引号对应的图片代码:pictureBox ...

  9. css的一种预处理器 sass

    之前觉得关于css什么的没什么,后来让别人给问住了...然后就悲催了... sass是一种css的预处理器,是一种函数式的css的编程: 主要还是看官网 http://www.w3cplus.com/ ...

  10. mysql——查询练习

    Sutdent表的定义 字段名 字段描述 数据类型 主键 外键 非空 唯一 自增 Id 学号 INT(10) 是 否 是 是 是 Name 姓名 VARCHAR(20) 否 否 是 否 否 Sex 性 ...