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. [bzoj 1594]猜数游戏

    主要是怎么处理矛盾 矛盾的条件有$2$种: 第一种是当把所有相等的$a$都全部找到后,他们并没有全联通,所以矛盾,因为没有两个是相同的 第二种是在2组$(l,r,a)$,$(l1,r1,a1)$中,$ ...

  2. Pycharm中一些不为人知的技巧

    工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半功倍的效果. 以下操作都是基于 Wind ...

  3. HDU1166:敌兵布阵(线段树模板)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. poj2060——Taxi Cab Scheme(最小路径覆盖)

    Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...

  5. udhcpd源码分析3--IP租赁管理

    1:重要的结构体 全局链表的成员struct dhcpOfferedAddr *leases 记录了当前租赁出去的IP信息 /* leases.h */ struct dhcpOfferedAddr ...

  6. *和&的使用

    给变量起一个别名: int a = 2; int &b = a; 取a的地址,实参是一个指针: void chage(int *data) { } void main() { int a = ...

  7. JVM之字节码执行引擎

    方法调用: 方法调用不同于方法执行,方法调用阶段唯一任务就是确定被调用方法的版本(即调用哪一个方法),暂时还不执行方法内部的具体过程.方法调用有,解析调用,分派调用(有静态分派,动态分派). 方法解析 ...

  8. ConvexScore

    题目描述 You are given N points (xi,yi) located on a two-dimensional plane. Consider a subset S of the N ...

  9. Long Parameter List(过长参数列)---要重构的味道

      一个函数,它的参数过多是不好的,不好维护和修改,易读性也差,容易出错.       消除过长参数的方法,有如下:        1.在面向对象中,你可以传递一个对象给函数,函数通过访问对象来获得参 ...

  10. 【BZOJ4903】【CTSC2017】吉夫特 [DP]

    吉夫特 Time Limit: 15 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行一个整数n. 接下 ...