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的更多相关文章

  1. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

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

  2. 【leetcode刷题笔记】Longest Valid Parentheses

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

  3. Valid Parentheses & Longest Valid Parentheses

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

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

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

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

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

  6. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

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

  7. [Leetcode] longest valid parentheses 最长的有效括号

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

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

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

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

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

随机推荐

  1. 【Java二十周年】Delphi转行java的一些小感触

    本文纯属一届小码农对java使用过程的体验感触 目录: 初遇java编程语言 与java的擦肩 深入java 跨平台性 开源支持 web的支撑 初遇java编程语言 刚上大学的时候,完全是个电脑盲.刚 ...

  2. [shiro学习笔记]第四节 使用源代码生成Shiro的CHM格式的API文档

    版本为1.2.3的shiro API chm个事故文档生成. 获取shiro源代码 编译生成API文档 转换成chm格式 API 获取shiro源代码 shiro官网: http://shiro.ap ...

  3. Dynamics CRM2016 升级老版本报“JavaScript Web 资源包含对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决办法

    今天在新的服务器上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了如下错误,向导检测到了我的JavaScript Web 资源中包含 ...

  4. cassandra eclipse 环境构建

    摘要 本文主要介绍如何在eclipse中搭建cassandra环境 更多cassandra,nosql 相关知识请访问http://www.webpersonaldeveloper.cn 正文 1.f ...

  5. 2.Cocos2dx 3.2中的重力系统Box2D

     1 添加Box2D相关的库 步骤1:右击项目所在的解决方案à添加->现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D ...

  6. Nginx的负载均衡 - 一致性哈希 (Consistent Hash)

    Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减 ...

  7. Linux的sleep()和usleep()

    1.sleep和usleep都是linux中的程序挂起函数.只是时间的单位不一样. 2. sleep的基本单位是s(秒),也可以用m(分).h(小时). 例: sleep 1 : 挂起1秒 sleep ...

  8. Android开发学习之路--React-Native之初体验

      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...

  9. C++ string类型占几个字节

          在C语言中我们操作字符串肯定用到的是指针或者数组,这样相对来说对字符串的处理还是比较麻烦的,好在C++中提供了 string 类型的支持,让我们在处理字符串时方便了许多.这篇文章并不是讲解 ...

  10. TDD实践感悟

    每个开发者都想开发出高质量的代码,更少的Bug.更容易维护不仅让人心情愉悦,也让我们有更多时间去学习和生活. 少加一些班,多陪家人,:) 当开发任务非常简单时,比如基本的增删改查,可能使用怎样的方式开 ...