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.
看到括弧配對的問題,就想到用 stack
那這次複雜一點是,要去算最長的 valid parentheses 長度
想法如下
- stack 存的不是字元,而是該字元的 index,用來找到匹對後,計算長度用
- 因為是要找最長valid parentheses,所以多用一個變數記錄目前最早的合法 index
c++
kotlin
沒有留言:
張貼留言