32.Longest-Valid-Parentheses
32. Longest Valid Parentheses
题目地址
https://leetcode.com/problems/longest-valid-parentheses/
https://www.cnblogs.com/grandyang/p/4424731.html
题目描述
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 "()()"代码
Approach 1: Dynamic Programming
dp[i] 代表 s[i]
Approach #2 Dyanmic Programming
dp[i] 代表 s[i]
) ( ( xxxxx ) )
dp[j - 1] j dp[i-1] / i-1 dp[i] / i
int j = i - 1 - dp[i - 1]; 是为dp[i]的左括号
Approach #2 Using Stack
Time complexity && Space complexity: O(n)
Approach #3 Without extra space
Last updated
Was this helpful?