Blog Archive

Tuesday, September 30, 2014

[Leetcode] Longest Valid Parentheses @Python - Sumnous's Blog

[Leetcode] Longest Valid Parentheses @Python - Sumnous's Blog: " if s == '' or s == '(' or s == ')':
return 0
stack = [(-1, ')')]
maxLen = 0
for i in xrange(len(s)):
if s[i] == ')' and stack[-1][1] == '(':
stack.pop()
maxLen = max(maxLen, i - stack[-1][0])
else:
stack.append((i, s[i]))
return maxLen"



'via Blog this'

No comments:

Post a Comment