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. 三大linux系统对比

    概述: centos作为服务器部署是第一选择.CentOS去除很多与服务器功能无关的应用,系统简单但非常稳定,命令行操作可以方便管理系统和应用,丰富的帮助文档和社区的支持. ubuntu最佳的应用领域 ...

  2. 字符串模式匹配算法--BF和KMP详解

    1,问题描述 字符串模式匹配:串的模式匹配 ,是求第一个字符串(模式串:str2)在第二个字符串(主串:str1)中的起始位置. 注意区分: 子串:要求连续   (如:abc 是abcdef的子串) ...

  3. HDU5154拓扑排序

    Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. 利用枚举算法实现todoList:把对应项添加的内容列表

    功能: 点击城市列表项,如果内容列表不存在,则插入点击项: 如果内容列表中已存在,则不插入,然后把内容列表中的对应项放到第一位. HTML代码: <!DOCTYPE html> <h ...

  5. ZooKeeper开发者指南(五)

    引言 这个文档是为了想利用ZooKeeper的协调服务来创建分布式应用的开发者提供的指南.它包括概念和实践的信息. 这个文档的一开始的的四部分呈现了不同ZooKeeper高级概念的的讨论.理解Zook ...

  6. vijos 1655 萌萌的糖果博弈 博弈

    背景 用糖果来引诱小朋友学习是最常用的手法,绵羊爸爸就是用糖果来引诱萌萌学习博弈的. 描述 他把糖果分成了两堆,一堆有A粒,另一堆有B粒.他让萌萌和他一起按照下面的规则取糖果:每次可以任意拿走其中一堆 ...

  7. 【BZOJ】1578: [Usaco2009 Feb]Stock Market 股票市场

    [题意]给定s个股票和d天,给出价格矩阵s*d,每天可以买入或卖出整数倍股票,初始资金m,求最大利益.m<=200000,s<=50,d<=10. [算法]完全背包 [题解]关键在于 ...

  8. bzoj 1058 bst

    因为是数列的维护,所以我们可以考虑用splay来维护,每次在x插入的时候就在x+1前面插入就行了,然后用bst来维护两问的答案,但是应该会tle.我们来考虑这个问题的性质,首先因为这个数列没有删除操作 ...

  9. hdu 1241Oil Deposits(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  10. ORA-02291:parent key not found

    Hibernate operation: Could not execute JDBC batch update; SQL [insert into dchnpricecarchancesource ...