惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - freephp

马斯克都在用的"第一性原理":为什么90%的程序员在"卷框架",而高手只看一件事? 一个复杂的问题是如何被化解的 开了 TUN 模式还是直连?90% 的人都踩过这个坑 为什么很多技术人越努力,越没价值? 睡前讲一段docker编译镜像的故事 换一个思维解决问题:希望在转角 企业级LLM已经到了next level:LangChain + DeepSeek = 王炸 发展的眼光看问题 人人都需要重视的Prompt Engineering 坚持写作和坚持思考是同样重要的 关注一波AWS Aurora AWS学习笔记之Lambda执行权限引发的思考 体验国产系统Deepin:很爽 细聊滑动窗口 需要怎么才能过好这一生 数据结构学习之树结构 从《一兆游戏》学到的知识点 我的日常AI使用 移位操作搞定两数之商 Git常用命令整理
最长有效括号子串问题
freephp · 2025-07-26 · via 博客园 - freephp

周末刷一下算法题,刚好遇到一道有趣的匹配子串问题。原题描述如下:

给定一个只包含字符 '(' 和 ')' 的字符串,返回其中 最长的有效(格式正确的)括号子串的长度。

示例 1:
输入:s = "(()"
输出:2
解释:最长的有效括号子串是 "()"。

示例 2:
输入:s = ")()())"
输出:4
解释:最长的有效括号子串是 "()()"。

示例 3:
输入:s = ""
输出:0

约束条件:
0 <= s.length <= 3 * 10⁴

s[i] 仅为 '(' 或 ')'。

先想到用一个栈记录左括号“(”和右括号“)”来维护不匹配括号的位置,后面再思考了一下这个算法不够优秀,完全可以把这个问题变成一个简单的数学归纳问题。从左往右匹配一遍,再从右往左匹配一遍,这样才不会遗漏所有的匹配子串。
用Python实现起来非常方便,代码如下所示:

class Solution:
    def longestValidParentheses(self, s: str) -> int:
        max_len = 0
        left = right = 0
        # Left to right scan it
        for char in s:
            if char == '(':
                left += 1
            else:
                right += 1
            if left == right:
                max_len = max(max_len, 2 * right)
            elif right > left:
                left = right = 0

        # Right to left scan it
        left = right = 0
        for char in reversed(s):
            if char == ')':
                right += 1
            else:
                left += 1
            if left == right:
                max_len = max(max_len, 2 * left)
            elif left > right:
                left = right = 0
      
        return max_len

其实在max_len = max(max_len, 2 * left)或者 max_len = max(max_len, 2 * right)都是一样的,因为这个时候都是left等于right,但出于自然理解的逻辑才写成如上所示。还可以考虑用动态规划的方法来解决这个问题,但空间复杂度也会更高。
多思考,多写代码,防止中年摆烂。