文章目录


数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/longest-valid-parentheses/
数据结构算法操作试题(C++/Python)——最长有效括号

2. 解答

python:56ms, 12.7MB, 50%

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

其他方法看 leetcode 链接 评论区~

相关文章:

  • 2021-10-13
  • 2022-01-24
  • 2022-12-23
  • 2021-09-17
  • 2021-10-06
  • 2021-05-20
  • 2021-04-25
  • 2021-06-21
猜你喜欢
  • 2022-01-23
  • 2022-01-29
  • 2021-12-09
  • 2022-12-23
  • 2021-11-05
  • 2021-06-13
相关资源
相似解决方案