当前位置:网站首页>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!
边栏推荐
- 配合蓝牙打印的encoding-indexes.js文件内容:
- pytorch basic operations: classification tasks using neural networks
- Luogu mini game Daquan (everyone who uses Luogu must know)
- C语言中i++和++i在循环中的差异性
- APP Bluetooth connection test of test technology
- JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
- 【漫画】2021满分程序员行为对照表(最新版)
- Redis database
- 25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
- Difference and analysis of CPU usage and load
猜你喜欢
51 MCU Peripherals: Infrared Communication
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
Block elements, inline elements (
elements, span elements)eggjs controller层调用controller层解决方案
使用TinkerPop框架对GDB增删改查
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
Stress testing and performance analysis of node projects
Double for loop case (use js jiujiu printing multiplication table)
Redis(十一) - 异步优化秒杀
网安学习-内网渗透4
随机推荐
21 Day Learning Challenge Schedule
HCIP第十七天
MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
A list of 300+ learning resources compiled by senior engineers of the Tao Department (the latest version in 2021)
PSQL function, predicate, CASE expression and set operations
Constructors, member variables, local variables
虚拟现实房产展示系统提前预见未来装修效果
PIL与numpy格式之间的转换
pytorch常用函数
程序员最重要的能力是什么?
H5 access payment process - WeChat payment & Alipay payment
165.比较版本号
在腾讯做外包测试的那些日子.....
Home NAS server (4) | MergerFS and SnapRaid data backup
nacos registry
Shell 脚本不同玩法
[OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
洛谷小游戏大全(用洛谷的人都得知道)
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
eggjs controller层调用controller层解决方案