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. UISearchController替换UISearchDisplayController

    随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...

  2. android 集成微博常见问题

    我们在做微博集成登录.分享.聊天的时候,肯定会遇到很多的坑,这里总结下常见的问题. 文件不存在 C8998 的解决方法 如图我们走微博授权登录的时候如果OAuth2.0 授权设置回调页面设置和本地的不 ...

  3. FORM中读取图片

    1.创建ITEM 重要属性如下 item属性:图像 大小样式:调整 数据库项:否 2.读取触发器 在block级别,创建trigger READ_IMAGE_FILE('D:\'||:XX_EMOLY ...

  4. Python模块探秘之EasyGui

    在Windows想用Python开发一些简单的界面,所以找到了很容易上手的EasyGui库.下面就分享一下简单的使用吧. 参考的链接:官网Tutorial 接下来,我将从简单,到复杂一点点的演示如何使 ...

  5. 14 fragment 创建

    静态展示 注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点 步骤一:创建一个类继承 Fragment 代码类型一: package com.fmy.demo1; im ...

  6. Android简易实战教程--第六话《开发一键锁屏应用2·完成》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51885687点击打开链接 上一篇,初步开发了这个应用,功能都有了(见http:// ...

  7. Android Service详解

    service作为四大组件值得我们的更多的关注 在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务.例如,一个从service播放音乐的音乐播放器,应 ...

  8. 如何在Cocos2D 1.0 中掩饰一个精灵(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 尝试编译运行,在你每一次点击屏幕的时候,你将看到我可爱的妻子制作 ...

  9. metasploit使用

    新版本的Metasploit分为Pro和Communicate版本,都可以使用WebUI的方式和Console的方式 下面主要介绍console方式的使用 1.   use

  10. [asp.net]登录协同工作平台安全解决方案

    [摘要]公司领导说登录验证的安全性如何保证,建议采用UKEY验证类似网银解决,调用第三方YT公司产品. 解决方案: 前端页面: <embed id="s_simnew61" ...