LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses
题目要求:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
这个题用‘栈’这种数据结构解决是比较方便的。程序如下:
class Solution {
public:
bool isValidPar(char left, char right)
{
if (left == '(' && right == ')')
return true;
if (left == '[' && right == ']')
return true;
if (left == '{' && right == '}')
return true; return false;
} bool isValid(string s) {
int sz = s.size();
stack<char> left;
for(int i = ; i < sz; i++)
{
char c = s[i];
if(c == '(' || c == '[' || c == '{')
left.push(s[i]);
else
{
if(left.size() == )
return false; char top = left.top();
if(!isValidPar(top, c))
return false; left.pop();
}
} return left.size() == ;
}
};
2. 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.
此文的动态规划解法参考自一博文:
1. 状态
DP[i]:以s[i-1]为结尾的longest valid parentheses substring的长度。
2. 通项公式
s[i] = '(':
DP[i] = 0
s[i] = ')':找i前一个字符的最长括号串DP[i]的前一个字符 j = i-2-DP[i-1]
DP[i] = DP[i-1] + 2 + DP[j],如果j >=0,且s[j] = '('
DP[i] = 0,如果j<0,或s[j] = ')'
......... ( x x x x )
j i-2 i-1
证明:不存在j' < j,且s[j' : i]为valid parentheses substring。
假如存在这样的j',则s[j'+1 : i-1]也valid。那么对于i-1来说:
( x x x x x
j' j'+1 i-1
这种情况下,i-1是不可能有比S[j'+1 : i-1]更长的valid parentheses substring的。
3. 计算方向
显然自左向右,且DP[0] = 0
具体代码如下:
class Solution {
public:
int longestValidParentheses(string s) {
int sz = s.size();
if(sz < )
return ; int maxLen = ;
vector<int> dp(sz + , );
for(int i = ; i < sz + ; i++)
{
int j = i - - dp[i - ];
if(s[i - ] == '(' || j < || s[j] == ')')
dp[i] = ;
else
{
dp[i] = dp[i - ] + + dp[j];
maxLen = max(maxLen, dp[i]);
}
} return maxLen;
}
};
LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses的更多相关文章
- 【LeetCode每天一题】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 ...
- Valid Parentheses & Longest Valid Parentheses
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [LeetCode] 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 [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 32. 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 ...
随机推荐
- [转]django-registration quickstart
Basic configuration and use--------------------------- Once installed, you can add django-registrati ...
- Hadoop学习笔记1:伪分布式环境搭建
在搭建Hadoop环境之前,请先阅读如下博文,把搭建Hadoop环境之前的准备工作做好,博文如下: 1.CentOS 6.7下安装JDK , 地址: http://blog.csdn.net/yule ...
- 协议系列之IP协议
1.协议 协议(protocol)的定义:为计算机网络中进行数据交换而建立的规则.标准或约定的集合.两个终端相互通信时双方达成的一种约定,规定了一套通信规则,双方通信必须遵守这些规则.这些规则规定了分 ...
- 18 Loader代码案例
目录结构: MainActivity.java 代码: package com.qf.day18_loader_demo2; import android.app.Activity; import a ...
- Spark技术内幕: Task向Executor提交的源码解析
在上文<Spark技术内幕:Stage划分及提交源码分析>中,我们分析了Stage的生成和提交.但是Stage的提交,只是DAGScheduler完成了对DAG的划分,生成了一个计算拓扑, ...
- struts1.x mvc简单例程
因为项目需要,需要使用struts1.x .因此学习下struts框架. 例程从struts 实现MVC框架入手,完成一次request请求. 一.前台 两个jsp文件 index.jsp: 只有一个 ...
- Cocoa惯性思维调试一例
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 人总有惯性思维,在编程调试里也不例外.你总以为错误是显然的那一 ...
- 【java集合框架源码剖析系列】java源码剖析之HashMap
前言:之所以打算写java集合框架源码剖析系列博客是因为自己反思了一下阿里内推一面的失败(估计没过,因为写此博客已距阿里巴巴一面一个星期),当时面试完之后感觉自己回答的挺好的,而且据面试官最后说的这几 ...
- Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)
ViewAnimator继承了FrameLayout,多个组件重合在一起,可以加入多个组件,然后切换的时候会有动画. ViewAnimator及其子类的继承关系 ViewAnimator常用属性 Vi ...
- Hessian探究(一)Hessian与springMVC结合
上一篇博客Hessian探究(一)Hessian入门示例我们初步简单的介绍了一下Hessian的使用入门示例,我们是通过Servlet来暴露Hessian的对外服务的,接下来我们介绍一下通过Sprin ...