涉及到括号判断用栈做即可
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
maxlen = 0
left = []
l1 = len(s)
last = -1
for i in range(l1):
if s[i] =='(':
left.append(i)
else:
if not left:
last = i
else:
left.pop()
if left:
maxlen = max(maxlen,i-left[-1])
else:
maxlen = max(maxlen,i-last)
return maxlen