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

 

相关文章:

  • 2021-06-25
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-06-27
  • 2021-10-09
  • 2021-08-12
  • 2021-09-25
  • 2021-06-11
相关资源
相似解决方案