-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem32.py
More file actions
55 lines (46 loc) · 1.4 KB
/
problem32.py
File metadata and controls
55 lines (46 loc) · 1.4 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
47
48
49
50
51
52
53
54
55
'''
32. Longest Valid Parentheses
Hard
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Example 2:
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
Solution:
Use deque to perform as stack
Description are as below in code.
'''
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
from collections import deque
if len(s) < 2:
return 0
stack = deque([-1])
max_length = 0
for i in range(len(s)):
print stack
if s[i] == '(':
#Record the last ('s position appears.
stack.append(i)
else:
stack.pop()
if not stack:
#Current character is )
#If the stack is empty, which means -1 is popped out
#that is invalid.
#So append the new beginning.
stack.append(i)
else:
#If not empty, which is the last ( position.
max_length = max(max_length, i - stack[-1])
return max_length
s = Solution()
print s.longestValidParentheses(")))))")