当前位置:网站首页>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!
边栏推荐
猜你喜欢
pytorch basic operations: classification tasks using neural networks
leetcode一步解决链表合并问题
Alluxio为Presto赋能跨云的自助服务能力
51 MCU peripherals: ADC
H5 access payment process - WeChat payment & Alipay payment
Block elements, inline elements (
elements, span elements)BGP experiment (route reflector, federation, route optimization)
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
leetcode一步解决链表反转问题
The advantages of making web3d dynamic product display
随机推荐
PIL与numpy格式之间的转换
配合蓝牙打印的encoding-indexes.js文件内容:
非关系型数据库MongoDB的特点及安装
BGP实验(路由反射器,联邦,路由优化)
51单片机外设篇:红外通信
Features and installation of non-relational database MongoDB
C语言基础知识梳理总结:零基础入门请看这一篇
【漫画】2021满分程序员行为对照表(最新版)
Timing task library in the language use Cron, rounding
Different ways of shell scripting
家用 NAS 服务器(4)| MergerFS和SnapRaid数据定时备份
服务器的单机防御与集群防御
Difference and analysis of CPU usage and load
Block elements, inline elements (
elements, span elements)Deep learning - CNN realizes the recognition of MNIST handwritten digits
5款经典代码阅读器的使用方案对比
51单片机外设篇:DS18B20
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
回文串求解的进阶方法
机器学习——支持向量机原理