```
def longestValidParentheses(self, s):
l = []
res = 0
for i in s:
if i == '(':
if not l or l[-1] == 0 or l[-1] != int(l[-1]):
l.append(0)
else:
l[-1] += .5
else:
if l:
if l[-1] == 0:
l[-1] = 2
else:
if l[-1] != int(l[-1]):
l[-1] = int(l[-1]) + 2
elif len(l) > 1:
last = int(l.pop())
l[-1] = int(l[-1])+last+2
else:
res = max(res, l[0])
l = []
return res if not l else max(res, int(max(l)))
```