当前位置:网站首页>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)
总结:这是一道典型的括号匹配题,从栈到动态规划,用空间换时间,大家可以反复观看,一定要吃透!
边栏推荐
- leetcode每天5题-Day04
- MySql copies data from one table to another table
- Mysql常用命令大全
- Browser onload event
- About the directory structure of the web application
- Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
- Google 安装印象笔记剪藏插件
- MySQL 8.0.29 set and modify the default password
- 【解决】RESP.app 连接不上redis
- Redis数据库
猜你喜欢

说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~

ELK log analysis system

ATM系统

Android studio connects to MySQL and completes simple login and registration functions

MySql copies data from one table to another table

Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)

Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer

navicat无法连接mysql超详细处理方法

navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)

对node工程进行压力测试与性能分析
随机推荐
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
H5接入支付流程-微信支付&支付宝支付
Google Chrome(谷歌浏览器)安装使用
【解决】RESP.app 连接不上redis
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
配合蓝牙打印的encoding-indexes.js文件内容:
165.比较版本号
Three methods of importing sql files in MySQL
leetcode 204. Count Primes 计数质数 (Easy)
100 latest software testing interview questions in 2022, summary of common interview questions and answers
C language: Check for omissions and fill in vacancies (3)
25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
网安学习-内网渗透4
Block elements, inline elements (
elements, span elements)About the directory structure of the web application
ATM系统
Google 安装印象笔记剪藏插件
coredns介绍