【题目解析】
对所有的 “(” 符号,分割连续子串的 “)” 符号建立索引。
class Solution:
def longestValidParentheses(self, s: str) -> int:
if not s:
return 0
index_list = [-1]
max_length = 0
for i, c in enumerate(s):
if c == "(":
index_list.append(i)
else:
index_list.pop()
if index_list== []:
index_list.append(i)
else:
max_length = max(max_length, i-index_list[-1])
return max_length