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 ...
随机推荐
- ELK平台的搭建
ELK是指Elasticsearch + Logstash + Kibaba三个组件的组合.本文讲解一个基于日志文件的ELK平台的搭建过程,有关ELK的原理以及更多其他信息,会在接下来的文章中继续研究 ...
- activiti 数据库升级 upgrade
分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519) 在项目中我们如果使用activiti 工作流引擎的时候,肯定是需要 ...
- ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot
Running Rosbridge Description: This tutorial shows you how to launch a rosbridge server and talk to ...
- Erlang标准数据结构的选择
Erlang标准数据结构的选择(金庆的专栏)gen_server with a dict vs mnesia table vs etshttp://stackoverflow.com/question ...
- dos2unix批量转换的一种方法
Linux本身提供了dos2unix和unix2dos两个命令来实现Windows和Linux文件的转换. 少量文件转换: 对于单个或少量的文件转换,可以直接使用命令,如: dos2unix file ...
- 浅谈SSH框架
在学习或者接触一个新的概念的时候,我们应该在脑海中发挥我们的搜索引擎,牵一发动全身的去想,这个知识跟我之前接触过的有哪些相同或者不同的地方,从这个角度去看那些新的知识和概念,经过旧知识和新知识的对比我 ...
- iOS开发出错whose view is not in the window hierarchy!的解决
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个简单的单窗口App在运行时出现错误: 2016-04-07 ...
- oracle中动态SQL详解
部分内容参考网上资料 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情 ...
- python 远程调度进程服务与客户端
python 远程调度进程服务与客户端 核心思想: 在本地或远程机器上创建一个进程,提供调度服务.使用了 APScheduler. 安装:APScheduler $ wget https://pypi ...
- UNIX网络编程——客户/服务器程序设计示范(三)
TCP预先派生子进程服务器程序,accept无上锁保护 我们的第一个"增强"型服务器程序使用称为预先派生子进程的技术.使用该技术的服务器不像传统意义的并发服务器那样为每个客户现场派 ...