当前位置:网站首页>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)
总结:这是一道典型的括号匹配题,从栈到动态规划,用空间换时间,大家可以反复观看,一定要吃透!
边栏推荐
- 说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
- Go language study notes - grpc serverclient protobuf Go language from scratch
- 关于web应用的目录结构
- C竞赛训练
- 提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次
- The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26
- APP Bluetooth connection test of test technology
- 浏览器的onload事件
- Cyber Security Learning - Intranet Penetration 4
- goroutine (coroutine) in go language
猜你喜欢

H5 access payment process - WeChat payment & Alipay payment

Detailed explanation of the software testing process (mind map) of the first-tier manufacturers

6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略

非关系型数据库MongoDB的特点及安装
![[PSQL] window function, GROUPING operator](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] window function, GROUPING operator

About the directory structure of the web application

Packaging and deployment of go projects

goroutine (coroutine) in go language

整合ssm(一)

无代码生产新模式探索
随机推荐
自动化运维工具——ansible、概述、安装、模块介绍
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
C 竞赛——捕鱼
Matlab论文插图绘制模板第41期—气泡图(bubblechart)
MySql将一张表的数据copy到另一张表中
[PSQL] 函数、谓词、CASE表达式、集合运算
Block elements, inline elements (
elements, span elements)51 microcontroller peripherals article: dot-matrix LCD
Timing task library in the language use Cron, rounding
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
51单片机外设篇:点阵式LCD
分布式文件存储服务器之Minio对象存储技术参考指南
nacos注册中心
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
51 MCU Peripherals: Infrared Communication
为什么4个字节的float要比8个字节的long大呢?
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
C language entry combat (13): decimal number to binary
Redis database