码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Longest Valid Parentheses python

时间:2015-12-16 00:23:48      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

class Solution(object):
    def longestValidParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        maxlen=0
        stack=[]
        last=-1
        for i in range(len(s)):
            if s[i] == (:
                stack.append(i)
            else:
                if stack == []:
                    last=i
                else:
                    stack.pop()
                    if stack == []:
                        maxlen=max(maxlen,i-last)
                    else:
                        maxlen=max(maxlen,i-stack[len(stack)-1])
        return maxlen

 

leetcode Longest Valid Parentheses python

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5049899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!