32. Longest Valid Parentheses
2018-03-13 15:26
417 查看
Given a string containing just the characters
def longestValidParentheses(self, s):
s = ')' + s
dp = [0] * len(s)
for i in range(1, len(s)):
if s[i] == ')' and s[i - dp[i - 1] - 1] == '(':
dp[i] = dp[i - 1] + 2 + dp[i - 2 - dp[i - 1]]
return max(dp)
'('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.
def longestValidParentheses(self, s):
s = ')' + s
dp = [0] * len(s)
for i in range(1, len(s)):
if s[i] == ')' and s[i - dp[i - 1] - 1] == '(':
dp[i] = dp[i - 1] + 2 + dp[i - 2 - dp[i - 1]]
return max(dp)
相关文章推荐
- 个人记录-LeetCode 32. Longest Valid Parentheses
- Leetcode 32. Longest Valid Parentheses (Hard) (cpp)
- leetcode 32. Longest Valid Parentheses 最长有效括号长度
- LeetCode 32. Longest Valid Parentheses (Hard)
- leetcode 32. Longest Valid Parentheses
- LeetCode----32. Longest Valid Parentheses(最长有效括号)
- LeetCode Algorithms 32. Longest Valid Parentheses 题解
- leetcode: 32. Longest Valid Parentheses [✗]
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses 动态规划解法
- LeetCode 32. Longest Valid Parentheses(hard)
- 32. Longest Valid Parentheses(最长括号匹配,hard)
- 32. Longest Valid Parentheses
- 【栈/DP】LeetCode 32. Longest Valid Parentheses 八种解法
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- LeetCode Algorithms 32. Longest Valid Parentheses
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)