思路
1.t:初始化栈,根据’(‘、’)‘匹配,遇’(‘入栈,遇’)':
匹配,则出栈,此时标识已匹配的字符
2.遍历原字符串s,最长连续标识即为最长有效括号的长度
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
if len(s)==0 or len(s)==1:
return 0
t=[]
s=list(s)
n=0
s1=0
for i in range(len(s)):
if s[i]=='(':
t.append(i)
else:
if len(t)>0:
j=t.pop()
s[j]=1
s[i]=1
for i in range(len(s)):
if s[i]==1:
s1+=1
n=max(n,s1)
else:
s1=0
return n