-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.py
More file actions
46 lines (41 loc) · 1.05 KB
/
32.py
File metadata and controls
46 lines (41 loc) · 1.05 KB
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
36
37
38
39
40
41
42
43
44
45
46
def getStack(s,stack):
for i in s:
if i == '(':
stack+=(i)
elif i == ')':
if stack and stack[-1] == '(':
stack=stack[:-1]
stack+='2'
else:
stack+=i
else:
stack=stack[:-1]+str(i)+stack[-1]
if s==stack:
return stack
print stack
return getStack(stack,'')
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
stack = []
count = 0
maxcount = 0
for i in s:
if i == '(':
stack.append(i)
elif i == ')':
if stack and stack[-1] == '(':
stack.pop(-1)
count +=2
if maxcount<count:
maxcount=count
else:
stack=[]
count = 0
print stack
return maxcount
solu = Solution()
print solu.longestValidParentheses("()(()")