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.
For loop:本题从左到右扫描一次(类似于排列组合里的楼梯)并计录当前段。扫描过程中左括号时刻不能比右括号少,否则重新计数。此外当左右括号相等时为一个有效括号序列,可以用来更新maxLen。
从右到左扫描的时候反之。