题目:
题解:
class Solution:def longestValidParentheses(self, s: str) -> int:stack=[]maxL=0n=len(s)tmp=[0]*n #标记数组cur=0for i in range(n):if s[i]=='(':stack.append(i)else:if stack:j=stack.pop()if s[j]=='(':tmp[i],tmp[j]=1,1 #匹配成功时标记 for num in tmp: #计算连续1出现的最大次数if num:cur+=numelse: #遇到0时中断,进行对比,并重置maxL=max(cur,maxL) cur=0maxL=max(cur,maxL) #最后一次统计可能未终断,多做一次对比return maxL