当前位置:网站首页>最长有效括号的问题
最长有效括号的问题
2022-06-09 18:52:00 【GreyZeng】
作者: Grey
原文地址:最长有效括号的问题
题目链接
主要思路
设置dp数组,长度和原始字符串的长度一样,
dp[i]表示:必须以i位置字符结尾的字符串的最长有效括号子串的长度是多少。
显然有:
dp[0] = 0; // 必须以0位置的字符结尾的最长有效括号子串是0
dp[1] = (str[1] == ')' && str[0] == '('?2:0); // 必须以1位置的字符结尾的最长有效括号子串,如果满足`()`则为2,否则都是无效子串,返回0
然后看任意一个普遍位置i
如果i位置的字符是(,则
dp[i] = 0
因为一个有效的括号子串的结尾不可能是(
如果i位置的字符是),则要分以下几种情况,假设我们以i=6为例,如下示例图

此时,如果i-1即5位置是(,如下示例

那么i位置的一种决策是:i位置和i-1先组成一个有效括号子串,长度是2,然后加上dp[i-2]的值,即:
dp[i] = dp[i-2] + 2
如果i-1位置,即5位置是),如下示例:

假设dp[i-1]即:dp[5] = 4,即str[2]位置一定是(,如下图

此时,分两种情况,如果str[1]位置上是(,即:

那么此时:
dp[6] = dp[5] + 6位置上的一个右括号+1位置上的一个左括号 + dp[0],即:dp[i] = dp[i-1] + 2 + dp[(i-1) - dp[i-1] - 1]
如果str[1]位置上是),即:

那么此时,dp[1]一定等于0,因为如果dp[1]不等于0,那么就意味着以1结尾的最长有效括号子串大于0,那么dp[5]就不止可以扩到2位置了,与我们假设的条件矛盾,所以,当dp[6]为),且dp[1]为)的时候,dp[6]一定等于0。
自此,所有可能性分析完毕。完整代码如下:
public static int longestValidParentheses(String s) {
if (s == null || s.length() <= 1) {
return 0;
}
char[] str = s.toCharArray();
// dp[i]:必须以i位置符号结尾的字符串,最长有效括号串长度是多少
int[] dp = new int[str.length];
// dp[0] = 0, dp[1] = 0
dp[1] = str[0] == '(' && str[1] == ')' ? 2 : 0;
int ans = dp[1];
for (int i = 2; i < str.length; i++) {
if (str[i] == ')') {
// 这才是有效情况
if (str[i - 1] == '(') {
dp[i] = dp[i - 2] + 2;
} else {
if ((i - 1) - dp[i - 1] >= 0 && str[(i - 1) - dp[i - 1]] == '(') {
dp[i] = dp[i - 1] + 2 + ((i - 1) - dp[i - 1] > 0 ? dp[(i - 1) - dp[i - 1] - 1] : 0);
}
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
更多
边栏推荐
- leetcode:剑指 Offer 56 - I. 数组中数字出现的次数【分组异或】
- 差分探头烧毁探头的原因
- Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]
- Using fingerprint for windows two factor identity authentication is both safe and convenient
- Notes on ad PCB drawing
- 2022年开什么实体店比较赚钱?适合女性做的小成本开店,叶其芳大健康
- DBeaver中如何调整SQL编辑器的字体大小
- 投资5.5亿元!默克宣布在中国张家港建先进半导体一体化基地
- Principle and implementation of avoiding group panic and load balancing
- C# 34. UdpClient transceiver
猜你喜欢

开关损耗测试方案中的示波器探头应用

为何罗氏探头测量高频电流会出现误差?

Google搜索为什么不能无限分页?

KVM virtualization Fundamentals

20220603怎么查询公网IP

SQL第四练:字符串处理函数

For 11 years, the programmer gave suggestions to undergraduate, graduate students and students preparing to engage in background development to learn the way to advance

Three annotations, elegant implementation of micro service authentication

测量电流探头如何降低噪音

20220529-mks格式的字幕怎么转换成srt、ass、ssa、idx格式的字幕.txt
随机推荐
C# 30. String interception
Three annotations, elegant implementation of micro service authentication
[SolidWorks detailed records] operation records of measuring method, setting the automatic elevation datum plane of sketch, setting the zoom in and zoom out direction of roller, adding thread line to
minikube 部署使用
循环结构程序设计2
嵌入式软件设计(中期总结)
Demagnetization and balance adjustment steps of oscilloscope current probe
[pb03f environment setup] Bluetooth 5.2 Anxin Ke pb-03f-kit development board secondary development environment setup
一季度全球PC GPU出货量下滑6.2%,疫情创造大量需求至此结束
Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]
Do your filial duty to make an old people's fall prevention alarm system for your family
Loop structure programming 2
Technology sharing | selenium multi browser processing
How to use wireless communication technology to optimize the fire water pipe network of iron and steel plant?
R|mapping. seq()
SQL exercise 4: string processing function
What software is foreign exchange transaction MT4? What is the difference between MT4 and MT5? What should I pay attention to when downloading MT4?
Ad delete dimension
一文彻底理解并发编程中非常重要的票据锁——StampedLock
投资5.5亿元!默克宣布在中国张家港建先进半导体一体化基地