您的位置:首页 > 编程语言 > Python开发

python写算法题:leetcode: 32. Longest Valid Parentheses

2017-07-29 11:13 483 查看
https://leetcode.com/problems/longest-valid-parentheses/#/description

class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: ins
"""
maxlen=0
startpos=0
maxres=[]
for p in xrange(len(s)):
if s[p] == '(':
maxres.append(startpos)
startpos=p+1
else:
if len(maxres)>0:
startpos=maxres[-1]
if maxlen<p-startpos+1:
maxlen=p-startpos+1
del maxres[-1]
else:
startpos=p+1

return maxlen

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: