leetcode Longest Valid Parentheses python

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

 

posted @ 2015-12-15 23:34  hao.ma  阅读(119)  评论(0编辑  收藏  举报