前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构算法操作试题(C++/Python)——最长有效括号

数据结构算法操作试题(C++/Python)——最长有效括号

作者头像
莫斯
发布2020-09-09 21:00:27
2940
发布2020-09-09 21:00:27
举报
文章被收录于专栏:备份备份

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


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/longest-valid-parentheses/

在这里插入图片描述
在这里插入图片描述

2. 解答

python:56ms, 12.7MB, 50%

代码语言:javascript
复制
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 链接 评论区~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录
  • 1. 题目
  • 2. 解答
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档