当前位置:网站首页>leetcode括号匹配问题——32.最长有效括号
leetcode括号匹配问题——32.最长有效括号
2022-08-02 05:08:00 【你食不食油饼】
题目描述:
1、栈
思路:首先看到括号,我们第一想法肯定是利用栈进行一个括号匹配,我们这道题也不例外;
我们先找到所有可以匹配的索引号,然后找出最长连续数列!
例如:s = )(()()),我们用栈可以找到,
位置 2 和位置 3 匹配,
位置 4 和位置 5 匹配,
位置 1 和位置 6 匹配,
这个数组为:2,3,4,5,1,6 这是通过栈找到的,我们按递增排序!1,2,3,4,5,6
找出该数组的最长连续数列的长度就是最长有效括号长度!
进入代码:
public int longestValidParentheses(String s) {
if (s.length() < 2) return 0;
Stack<Integer> stack = new Stack<>();
stack.push(-1);
int res = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(')
stack.push(i);
else {
stack.pop();
if (stack.isEmpty())
stack.push(i);
else
res = Math.max(res,i - stack.peek());
}
}
return res;
}
时间复杂度:O(n)
空间复杂度:O(1)
2、动态规划(空间换时间)
思路:当我们采用动态规划时,主要考虑几种情况:
当s.charAt(i) == '('时,dp[i] = 0;
反之s.charAt(i) == ')'时,我们又分:
当s.charAt(i - 1) == '('时,dp[i] = dp[i - 2] + 2;
反之当s.charAt(i - 1) == ')'时,又分:
当s.charAt(i - dp[i - 1] - 1) == '('时,dp[i] = dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]
反之dp[i] = 0;
进入代码:
public static int longestValidParentheses(String s) {
if (s.length() < 2) return 0;
int[] dp = new int[s.length()];
//第一个符号无论如何长度都为0
dp[0] = 0;
int max = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '(')
dp[i] = 0;
else
if (s.charAt(i - 1) == '(') {
dp[i] = i - 2 >= 0 ? dp[i - 2] + 2 : 2;
} else {
if (i - dp[i - 1] -1 >= 0 && s.charAt(i - dp[i - 1] - 1) == '(')
dp[i] = i - dp[i - 1] - 2>=0
? dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]
: dp[i - 1] + 2;
else dp[i] = 0;
}
max = Math.max(dp[i], max);
}
return max;
}
时间复杂度:O(n)
空间复杂度:O(n)
总结:这是一道典型的括号匹配题,从栈到动态规划,用空间换时间,大家可以反复观看,一定要吃透!
边栏推荐
- 自动化运维工具——ansible、概述、安装、模块介绍
- c语言:查漏补缺(三)
- 25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
- 51 MCU peripherals: ADC
- ATM系统
- mysql实现按照自定义(指定顺序)排序
- 18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
- Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
- 分布式文件存储服务器之Minio对象存储技术参考指南
- 面试测试工程师一般会问什么?测试主管告诉你
猜你喜欢
Meta公司内部项目-RaptorX:将Presto性能提升10倍
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
Google 安装印象笔记剪藏插件
51 MCU Peripherals: Infrared Communication
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
navicat新建数据库
navicat无法连接mysql超详细处理方法
21 Day Learning Challenge Schedule
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
随机推荐
网安学习-内网渗透4
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
21 Day Learning Challenge Schedule
Shuttle + Alluxio 加速内存Shuffle起飞
The Go language learning notes - dealing with timeout - use the language from scratch from Context
165.比较版本号
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
Constructors, member variables, local variables
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
How much does a test environment cost? Start with cost and efficiency
navicat新建数据库
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
21天学习挑战赛安排
Navicat cannot connect to mysql super detailed processing method
LeetCode刷题系列 -- 10. 正则表达式匹配
51单片机外设篇:DS18B20
APP Bluetooth connection test of test technology
leetcode一步解决链表反转问题
navicat无法连接mysql超详细处理方法