当前位置:网站首页>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!
边栏推荐
- TikTok平台的两种账户有什么区别?
- C语言入门实战(13):十进制数转二进制
- 双重for循环案例(用js打印九九乘法表)
- Redis集群模式
- There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?
- nacos registry
- 说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
- Shell 脚本不同玩法
- 51单片机外设篇:DS18B20
- What is the most important ability of a programmer?
猜你喜欢

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

51 MCU Peripherals: Infrared Communication

Google notes cut hidden plug-in installation impression

Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula

There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?

C语言中i++和++i在循环中的差异性

Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战

Machine learning -- - theory of support vector machine (SVM)

无代码生产新模式探索

软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
随机推荐
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
Redis集群模式
整合ssm(一)
如何优化OpenSumi终端性能?
Home NAS server (4) | MergerFS and SnapRaid data backup
C竞赛训练
APP Bluetooth connection test of test technology
kubernetes 亲和、反亲和、污点、容忍
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
The advantages of making web3d dynamic product display
51单片机外设篇:ADC
Use the browser's local storage to realize the function of remembering the user name
C语言小游戏——扫雷小游戏
51单片机外设篇:红外通信
Linux CentOS8安装Redis6
如何进行并发数计算(稳定性测试和压力测试)?
nacos registry
kubernetes affinity, anti-affinity, taint, tolerance
21 Day Learning Challenge Schedule
5款经典代码阅读器的使用方案对比