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

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 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,但出于自然理解的逻辑才写成如上所示。还可以考虑用动态规划的方法来解决这个问题,但空间复杂度也会更高。
多思考,多写代码,防止中年摆烂。