当前位置:网站首页>【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;
}
}

边栏推荐
- Wuzhicms code audit
- Developers, MySQL column finish, help you easily from installation to entry
- 网页游戏引擎
- wuzhicms代码审计
- How to choose one plus 10 pro and iPhone 13?
- 我写了一份初学者的学习实践教程!
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- Summary of tx.origin security issues
- [template] [Luogu p4630] duathlon Triathlon (round square tree)
- NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
猜你喜欢

【HCIA持续更新】WLAN工作流程概述

第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办

The Ministry of human resources and Social Security announced the new construction occupation

一加10 Pro和iPhone 13怎么选?

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

Rainfall warning broadcast automatic data platform bwii broadcast warning monitor

With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games

Datakit -- the real unified observability agent

【华为HCIA持续更新】SDN与FVC

NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
随机推荐
长城证券开户安全吗 证券账户怎么开通
利用win10计划任务程序定时自动运行jar包
R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
Is it safe for Bank of China Securities to open an account online?
Analysis of abnormal frequency of minor GC in container environment
[HCIA continuous update] WLAN overview and basic concepts
The Block:USDD增长势头强劲
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
MVC模式和三层架构
Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness
MD5加密的两种方式
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
居家打工年入800多万,一共五份全职工作,他还有时间打游戏
leetcode刷题目录总结
R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
DataKit——真正的统一可观测性 Agent
设置窗体透明 隐藏任务栏 与全屏显示