当前位置:网站首页>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!
边栏推荐
猜你喜欢

OAuth 授权协议 | 都云原生时代了,我们应该多懂一点OAuth ?

Redis集群模式

Features and installation of non-relational database MongoDB
C语言小游戏——扫雷小游戏

C语言基础知识梳理总结:零基础入门请看这一篇

51 microcontroller peripherals article: dot-matrix LCD

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

BGP experiment (route reflector, federation, route optimization)

BGP实验(路由反射器,联邦,路由优化)

【解决】RESP.app 连接不上redis
随机推荐
kubernetes 亲和、反亲和、污点、容忍
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
虚拟现实房产展示系统提前预见未来装修效果
Linux CentOS8安装Redis6
C竞赛训练
C语言中i++和++i在循环中的差异性
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
Polar Parametrization for Vision-based Surround-View 3D Detection Paper Notes
Install and use Google Chrome
高防服务器防御的原理是什么
程序员最重要的能力是什么?
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
Difference and analysis of CPU usage and load
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
TikTok平台的两种账户有什么区别?
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
Use the browser's local storage to realize the function of remembering the user name
51 microcontroller peripherals article: dot-matrix LCD
关于web应用的目录结构
关于 VS Code 优化启动性能的实践