当前位置:网站首页>【Hot100】32. 最长有效括号
【Hot100】32. 最长有效括号
2022-07-04 16:03:00 【王六六的IT日常】

参考:https://leetcode.cn/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode-solution/
参考:leetcode hot 100-32. 最长有效括号
- 动态规划
class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
int[] dp = new int[s.length()];
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == ')') {
if (s.charAt(i - 1) == '(') {
dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
} else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
}
maxans = Math.max(maxans, dp[i]);
}
}
return maxans;
}
}
- 栈
class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
Deque<Integer> stack = new LinkedList<Integer>();
stack.push(-1);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(i);
} else {
stack.pop();
if (stack.isEmpty()) {
stack.push(i);
} else {
maxans = Math.max(maxans, i - stack.peek());
}
}
}
return maxans;
}
}
- 不需要额外的空间
class Solution {
public int longestValidParentheses(String s) {
int left = 0, right = 0, maxlength = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = Math.max(maxlength, 2 * right);
} else if (right > left) {
left = right = 0;
}
}
left = right = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = Math.max(maxlength, 2 * left);
} else if (left > right) {
left = right = 0;
}
}
return maxlength;
}
}

边栏推荐
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- Redis 的内存淘汰策略和过期删除策略的区别
- Leetcode list summary
- 网页游戏引擎
- Developers, MySQL column finish, help you easily from installation to entry
- 【测试开发】软件测试——基础篇
- R language plot visualization: plot visualization of multiple variable violin plot in R with plot
- 【模板】【luogu P4630】Duathlon 铁人两项(圆方树)
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
猜你喜欢

Master the use of auto analyze in data warehouse

超标量处理器设计 姚永斌 第5章 指令集体系 摘录

Internet addiction changes brain structure: language function is affected, making people unable to speak neatly

7 RSA密码体制

What if Kaili can't input Chinese???

斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源

电子宠物小狗-内部结构是什么?

To sort out messy header files, I use include what you use

Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection

NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
随机推荐
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
图像检索(image retrieval)
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
[template] [Luogu p4630] duathlon Triathlon (round square tree)
高中物理:力、物体和平衡
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
What are cache penetration, cache breakdown, and cache avalanche
Difference between redis' memory obsolescence strategy and expiration deletion strategy
Implementation of super large-scale warehouse clusters in large commercial banks
正则表达式
Ble HCI flow control mechanism
RecastNavigation 之 Recast
About the pit of firewall opening 8848 when Nacos is started
Load test practice of pingcode performance test
网页游戏引擎
detectron2安装方法
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly