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. 不应滥用named let

    > (define (f x) x) > (define (g x) (let rec((x x)) x)) > (define a '(1 2 3)) > (f a) ( ) ...

  2. MapReduce:并行计算框架

    MapReduce 是 Hadoop 的核心组成,是专用于进行数据计算的.重点掌握实现 MapReduce 算法的步骤,掌握 map.reduce 函数的特点.如何写函数. 如果我们把 MapRedu ...

  3. JQuery 网页选项卡制作

    网页选项卡可以较好的利用有限的页面来展示更多的元素,而使用JQuery来制作网页选项卡也是一件非常简单的事情.今天就来分享一个网页选项卡的制作小技巧. 引入所需库 选项卡原理 业务核心 完整小例子 引 ...

  4. 10 GridView 样式属性

    GridView 样式属性: 1,android:numColumns="auto_fit" 显示的列数 如果android:numColumns不设置那么自动每行1列 如下图 2 ...

  5. Xcode7.2如何真机调试iOS 9.3的设备

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 本猫的mac系统为10.10,Xcode版本为7.2 本猫将i ...

  6. UI设计--->全心全意为人民服务的宗旨---->注重客户体验--->软件持久的生命力

    UI即User Interface(用户界面)的简称.UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性有品味,还要让软件的操作变得舒适简单.自由,充分体现 ...

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

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

  8. Android Demo 下拉刷新+加载更多+滑动删除

    小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...

  9. 精通CSS+DIV网页样式与布局--滤镜的使用

    在上篇博客中,小编主要简单的介绍了使用CSS,如何制作实用菜单,今天我们继续来总结有关CSS的基础知识,今天小编主要简单的来介绍一下CSS中关于滤镜的使用,首先,小编先来简单的介绍一下滤镜,我们这次来 ...

  10. iOS中 通知中心Text (实例)

    指定根视图 self.window.rootViewController = [RootViewController new]; 方法实现: #import "RootViewControl ...