LeetCode之“动态规划”: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

  具体代码如下:

 1 class Solution {
 2 public:
 3     int longestValidParentheses(string s) {
 4         int sz = s.size();
 5         if(sz < 2)
 6             return 0;
 7
 8         int maxLen = 0;
 9         vector<int> dp(sz + 1, 0);
10         for(int i = 1; i < sz + 1; i++)
11         {
12             int j = i - 2 - dp[i - 1];
13             if(s[i - 1] == ‘(‘ || j < 0 || s[j] == ‘)‘)
14                 dp[i] = 0;
15             else
16             {
17                 dp[i] = dp[i - 1] + 2 + dp[j];
18                 maxLen = max(maxLen, dp[i]);
19             }
20         }
21
22         return maxLen;
23     }
24 };
时间: 2024-10-27 05:29:12

LeetCode之“动态规划”:Longest Valid Parentheses的相关文章

[Leetcode][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo

LeetCode解题报告—— Longest Valid Parentheses

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

【leetcode】32. Longest Valid Parentheses

这题不能使用递归来写,因为有一个输入长度是17173,会爆栈.因此得手动模拟栈调用. public class Solution { private static class StackFrame { public final char left; private int num; public StackFrame(char left) { this.left = left; } @Override public String toString() { return "StackFrame [

[LeetCode] 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 &

Leetcode 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 &

leetcode 之 Longest Valid Parentheses

leetcode中和括号匹配相关的问题共有三个,分别是: 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 a

[LeetCode][JavaScript]Longest Valid Parentheses

https://leetcode.com/problems/longest-valid-parentheses/ 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

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] 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

leetcode第31题--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 &