边工作边刷题:70天一遍leetcode: day 35-1

Longest Valid Parentheses

错误点:

  • 遇到’)’后pop()的
class Solution(object):
    def longestValidParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        stk = []
        count=0
        maxLen=0
        left=0
        for i in range(len(s)):
            if s[i]=='(':
                stk.append((s[i],i))
            else:
                if not stk:
                    count=0
                    left=i+1
                else:
                    stk.pop()
                    if not stk:
                        count=i-left+1
                    else:
                        count=i-stk[-1][1]
                    if count>maxLen: maxLen = count
        
        return maxLen
                    
                
原文地址:https://www.cnblogs.com/absolute/p/5678224.html