-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongest_valid_parentheses.py
More file actions
35 lines (29 loc) · 909 Bytes
/
Copy pathlongest_valid_parentheses.py
File metadata and controls
35 lines (29 loc) · 909 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
29
30
31
32
33
34
35
class Solution:
# @param s, a string
# @return an integer
def longestValidParentheses(self, s):
validflag = [False] * len(s)
stack = []
for i in range(0, len(s)):
if s[i] == '(':
stack.append(i)
else:
if len(stack) == 0:
continue
else:
left = stack.pop()
validflag[left] = True
validflag[i] = True
maxlen = 0
curlen = 0
for i in range(len(validflag)):
if validflag[i]:
curlen += 1
else:
maxlen = max(maxlen, curlen)
curlen = 0
maxlen = max(maxlen, curlen)
return maxlen
if __name__ == '__main__':
print Solution().longestValidParentheses(')()())')
print Solution().longestValidParentheses('()')