题解 | 最长的括号子串

最长的括号子串

https://www.nowcoder.com/practice/45fd68024a4c4e97a8d6c45fc61dc6ad

from pickle import APPEND
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def longestValidParentheses(self , s: str) -> int:
        # write code here
        n = len(s)
        res = 0
        L = 0
        stack = []
        dic = {}
        minq,maxq = n ,-1
        for i in range(n):
            if stack:
                if s[i] ==")":
                    j = stack.pop()
                    dic[j] = 1
                    dic[i] = 1 
                    maxq = max(maxq,max(i,j))
                    minq = min(minq,min(i,j))
                else:
                    stack.append(i)
            else:
                if s[i] =="(":
                   stack.append(i)
        st = -1
        for i in range(minq,maxq+2):
            if st==-1:
                st = i
            if i not in dic:
                res = max(res,i-st)
                st = -1
        return res   


                

全部评论

相关推荐

牛至超人:您好,京东物流岗了解一下吗?负责精加工食品的端到端传输
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务