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.
这道题好理解,但是要自己想的话还是很难想出来的,动态规划说白了就是记录当前的结果,留给后面的用。
求最长合法匹配的长度,这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1] 包含s[i] 的最长的有效匹配括号子串长度。则存在如下关系:
- dp[s.length - 1] = 0;
 - i从n - 2 -> 0逆向求dp[],并记录其最大值。若s[i] == '(',则在s中从i开始到s.length - 1计算dp[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]。
class Solution {
public:
int longestValidParentheses(string s) {
int len=s.size();
vector<int >res(len,);
int finalRes=;
for(int i = len-;i>=;i--)
{
if(s[i]=='(')
{
int end=i + + res[i + ];
if(end<len&&s[end]==')')
{
res[i]=res[i+]+;
if(end+<len)
res[i]+=res[end+];
finalRes=max(finalRes,res[i]);
} }
}
return finalRes;
}
};最简单的方法:当碰到")"时,向左遍历找到未标记过的"(",则将这两个位置标为1,然后统计1连续出现的长度即可,时间复杂度为O(n^2)。
http://blog.csdn.net/cfc1243570631/article/details/9304525
 
 
Longest Valid Parentheses——仍然需要认真看看(动态规划)的更多相关文章
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
		
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
 - [LeetCode] Longest Valid Parentheses 动态规划
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - 动态规划——Longest Valid Parentheses
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - [LeetCode] Longest Valid Parentheses 最长有效括号
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - 【leetcode】 Longest Valid Parentheses  (hard)★
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - Longest Valid Parentheses   每每一看到自己的这段没通过的辛酸代码
		
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
 - 【Longest Valid Parentheses】cpp
		
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
 - Longest Valid Parentheses(最长有效括号)
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - LeetCode32 Longest Valid Parentheses
		
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
 
随机推荐
- UIColor延伸:判断两个颜色是否相等
			
不管UIColor使用CIColor,CGColor还是其他方式初始化的,其CGColor属性都是可用的.CoreGraphics中提供一个函数,用于判断两个CGColor是否相等,因此我们可以通过这 ...
 - Codeforces Round #337 (Div. 2)B
			
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - dns随笔(部分转载)
			
1.allow-notify allow-notify 定义了一个匹配列表并且只应用于从dns区域(slave zone),比如,这个列表是一个ip列表,它 2. 触发同步的过程 http://www ...
 - bzoj 1106 [POI2007]立方体大作战tet 树状数组优化
			
[POI2007]立方体大作战tet Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 821 Solved: 601[Submit][Status][ ...
 - Linux下find命令及其参数的使用
			
find命令原理:从指定的起始目录开始,递归地搜索其各个子目录,查找满足寻找条件的文件,并可以对其进行相关的操作. 格式:find [查找目录] [参数] [匹配模型] 多参数格式:find [查找目 ...
 - ObservableCollection 类
			
假设您正在创建 Windows 窗体应用程序,并且已将 DataGridView 控件绑定到标准 List(Of Customer) 数据结构.您希望能够使网格中的项目与基础数据源中的值保持同步.也就 ...
 - vue-cli中引入jquery的方法
			
vue-cli中引入jquery的方法 以前写vue项目都没有引入过jquery,今天群里面的一位小伙伴问了我这个问题,我就自己捣鼓了一下,方法如下: 我们先进入webpack.base.conf.j ...
 - 我的CCF备考指南
			
CCF计算机软件能力认证(简称CCF CSP认证). 认证涉及知识点: 认证内容主要覆盖大学计算机专业所学习的程序设计.数据结构.算法以及相关的数学基础知识.包括但不限于: (1)程序设计基础 逻辑与 ...
 - [LeetCode] 3. Longest Substring Without Repeating Characters ☆☆☆
			
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
 - PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
			
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...