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

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

思路

尝试遍历判断所有组合结果超时,想到了用dp来解这题却不知道怎么来建模,还是经验少了。

看了下解答,有多种解法,首先是dp。看了之后发现为什么自己的dp总是建不起来了,首先是确定一维的dp还是二维的dp,这题的变量有两个,一个是字符串的长度,还有就是最优解也就是最长合法parentheses substring的长度,那么用一维的dp数组dp[i]即可。接下来确定的是dp[i],中索引的 i 具体指的是什么,在考虑这步的时候我没有想清楚,与其说 i 具体指什么倒不如说我们要赋给 i 的意义是什么,我原来的想法是 i 代表从0到i的字串,这样来代表子问题,比如dp[5]表示的是从索引0到5的字串中最长合法 parentheses substring 的长度,dp[10] 表示从索引0到10的字串中最长合法 parentheses substring 的长度等。然后依次去探究递推公式时(问题与其相邻子问题的关系)却发现很不好找,因为

假如在 i 位置和 i-1 位置上的 字符是 ( 和 ),构成一对合法,但是还是确定不了 dp[i] 和 dp[i-2] 的关系,因为确定不了dp[i-2] 中的合法字符是不是在末尾 i-2 处终结的,如果是的话则dp[i]=dp[i-2]+2,否则不能确定判断是否加2。

上面的dp建模思路之所以不正确是因为建模没有清楚问题,或者说是限定问题,也可以说是建模对于问题的描述存在不清楚,不明确的地方。比如对于 dp[i] 虽然可以由此知道子串的最长合法字串的长度,但是由于这题是括号匹配,故子串中括号字符串出现的位置是很重要的,而dp[i]虽然可以确定 子问题的最优解,子问题中字符串的长度这两个变化的要素,但是却确定不了合法括号字符串在字串中出现的位置,所以导致实际解题时思路异常困难,因为本身的模型就有问题。

正确的 dp[i] 建模是这样的,值肯定是最长合法括号字符串的长度,而这个 i 代表的是以i位置为结束的最长合法字串,即dp[5]表示的是索引0到5的字符串中以索引5(末尾)为结束的最长的合法字串的长度。这样便能很方便的确定问题与相邻子问题之间的数学关系。

第二个可能有点难理解,因为 dp[i] 表示的是以索引 i 处为结束的最长合法字串的长度,那么它前一个字符的位置便是 i - dp[i]-1, 如果这个位置 是 ( 那么可能和新加入的 ) 构成合法,因为这个 ( 之前的字符串也可能是合法的,所以要在一起算。

只考虑以上的情况是因为如果以(( 或者)( 的话,以 ( 在末位置结束的肯定是非法,dp数组的是0.

代码

public class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
int dp[] = new int[s.length()];
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == ')') {
if (s.charAt(i - 1) == '(') {
dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
} else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
}
maxans = Math.max(maxans, dp[i]);
}
}
return maxans;
}
}

剩下的方式有两种,Using Stack 和 Without extra space: https://leetcode.com/problems/longest-valid-parentheses/solution/

LeetCode解题报告—— Longest Valid Parentheses的更多相关文章

  1. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  2. 【LeetCode练习题】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  3. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  4. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  5. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  6. 【LeetCode】32. Longest Valid Parentheses

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

  7. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  8. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  9. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

随机推荐

  1. MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)-Using index condition

    http://blog.itpub.net/22664653/viewspace-1210844/ -- 这篇博客写的更细,以后看 ICP(index condition pushdown)是mysq ...

  2. 【线性DP】【lgP1336】最佳课题选择

    传送门 Description Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具 ...

  3. js正则:两边字符固定,中间任意字符

    求些一个js正则!两边字符固定,中间任意字符.在一个长字符串里面匹配一小段,这一小段字符串开头和结尾都是固定的字符,就是中间是任意长度的字符.怎么写? /aa.+aa/ aa是你的固定字符,如果是反斜 ...

  4. 将shell返回的结果保存至数组

    如下,我需要将u1和u2提取出保存至数组,方便后续的调用 root@ubuntu:~# lxc list+------+---------+------------------------------ ...

  5. HDU4612:Warm up(缩点+树的直径)

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  6. unix网络编程-套接字编程 读书笔记

    1. 学习总结(目前只看了前6章):http://note.youdao.com/noteshare?id=2a0c29f5feeddd8f6f390427f0d67114 2. 课后习题 第一章 h ...

  7. Java并发多线程 - 并发工具类JUC

    安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...

  8. sql生成一个日期表

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...

  9. mysql的数据库 索引

    1.两种主要的引擎:MyISAM和InnoDB 2.如何查看自己的表是什么类型:http://www.cnblogs.com/luosongchao/archive/2013/05/23/309592 ...

  10. POJ 2891- Strange Way to Express Integers CRT 除数非互质

    题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...