当前位置:网站首页>Leetcode parentheses matching problem -- 32. The longest parentheses effectively
Leetcode parentheses matching problem -- 32. The longest parentheses effectively
2022-08-02 06:33:00 【You don't eat cake to eat】
Title description:

1. Stack
Idea: When we first see the parentheses, our first thought must be to use the stack to perform a parenthesis match, and our problem is no exception;
We first find all index numbers that can be matched, and then find the longest continuous sequence!
For example: s = )(()()), we can use the stack to find,
Position 2 and position 3 match,
Position 4 matches position 5,
Position 1 and position 6 match,
This array is: 2,3,4,5,1,6 This is found through the stack, we sort by increasing!1,2,3,4,5,6
Find the length of the longest continuous sequence of the array is the longest effective bracket length!
Entry code:
public int longestValidParentheses(String s) {if (s.length() < 2) return 0;Stack 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);elseres = Math.max(res,i - stack.peek());}}return res;} Time complexity: O(n)
Space complexity: O(1)
2. Dynamic programming (space for time)
Thinking:When we use dynamic programming, we mainly consider several situations:
When s.charAt(i) == '(', dp[i] = 0;
When s.charAt(i) == ')', we divide again:
When s.charAt(i - 1) == '(', dp[i] = dp[i - 2] + 2;
Conversely, when s.charAt(i - 1) == ')', it is divided into:
dp[i] = dp[i] = dp[i - 1] + 2 + dp[i - dp when s.charAt(i - dp[i - 1] - 1) == '('[i - 1] - 2]
dp[i] = 0;
Entry code:
public static int longestValidParentheses(String s) {if (s.length() < 2) return 0;int[] dp = new int[s.length()];//The first symbol has a length of 0 no matter whatdp[0] = 0;int max = 0;for (int i = 1; i < s.length(); i++) {if (s.charAt(i) == '(')dp[i] = 0;elseif (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;}Time complexity: O(n)
Space complexity: O(n)
Summary: This is a typical bracket matching question, from stack to dynamic programming, using space for time, you can watch it repeatedly, you must understand it thoroughly!
边栏推荐
- About the directory structure of the web application
- Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
- What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
- Cyber Security Learning - Intranet Penetration 4
- Redis集群模式
- 跨桌面端Web容器演进
- Redis database
- 整合ssm(一)
- Packaging and deployment of go projects
- 网安学习-内网渗透4
猜你喜欢

5款经典代码阅读器的使用方案对比

Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟

淘系资深工程师整理的300+项学习资源清单(2021最新版)

51 MCU peripherals: DS18B20

整合ssm(一)

无代码生产新模式探索

MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目

Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
随机推荐
Home NAS server (4) | MergerFS and SnapRaid data backup
Detailed explanation of interface in Go language
关于web应用的目录结构
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
5款经典代码阅读器的使用方案对比
虚拟现实房产展示系统提前预见未来装修效果
leetcode括号匹配问题——32.最长有效括号
配合蓝牙打印的encoding-indexes.js文件内容:
Redis-----非关系数据库
对node工程进行压力测试与性能分析
Luogu mini game Daquan (everyone who uses Luogu must know)
Thread Basics (1)
Shuttle + Alluxio 加速内存Shuffle起飞
C语言中i++和++i在循环中的差异性
51 MCU Peripherals: Infrared Communication
Meta公司内部项目-RaptorX:将Presto性能提升10倍
机器学习——支持向量机原理
程序员最重要的能力是什么?
selenium + robotframework的运行原理