-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path032.py
More file actions
28 lines (28 loc) · 723 Bytes
/
032.py
File metadata and controls
28 lines (28 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) < 2:
return 0
st, n = [0], len(s)
for i in range(1, n):
if s[i] == '(':
st.append(i)
else:
if st and s[st[-1]] == '(':
st.pop()
else:
st.append(i)
if st == []:
return n
if st[0] > 0:
ans = st[0]
else:
ans = 0
for i in range(1, len(st)):
ans = max(st[i]-st[i-1] - 1, ans)
if st[-1] < n:
ans = max(n-1 - st[-1], ans)
return ans