Leetcode每日一题——T32. 最长有效括号(难)——栈
程序员文章站
2022-03-06 21:00:04
...
法一:栈
class Solution:
def longestValidParentheses(self, s: str) -> int:
if not s:
return 0
res = 0
stack = [-1]
for i in range(len(s)):
if s[i] == '(':
stack.append(i)
else:
stack.pop()
if not stack: # 当stack为空时
stack.append(i)
else:
res = max(res, i - stack[-1])
return res
下一篇: LeetCode每日一题(题32)