当前位置:网站首页>【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;
}
}
边栏推荐
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- 居家打工年入800多万,一共五份全职工作,他还有时间打游戏
- MVC mode and three-tier architecture
- Developers, MySQL column finish, help you easily from installation to entry
- 数学分析_笔记_第7章:多元函数的微分学
- 你应该懂些CI/CD
- 完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
- 第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
- [acwing] 58 weeks 4489 Longest subsequence
- 【华为HCIA持续更新】SDN与FVC
猜你喜欢
Master the use of auto analyze in data warehouse
It's too convenient. You can complete the code release and approval by nailing it!
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
CocosCreator事件派發使用
7 RSA Cryptosystem
[Huawei HCIA continuous update] SDN and FVC
How to implement a delay queue?
Electronic pet dog - what is the internal structure?
Load test practice of pingcode performance test
第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
随机推荐
雨量预警广播自动化数据平台BWII 型广播预警监测仪
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Learn more about the basic situation of 2022pmp examination
Pytorch深度学习之环境搭建
【测试开发】软件测试——基础篇
CocosCreator事件派發使用
网页游戏引擎
【HCIA持续更新】WLAN工作流程概述
Image retrieval
整理混乱的头文件,我用include what you use
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
Go micro tutorial - Chapter 2 go micro V3 using gin and etcd
[HCIA continuous update] WLAN overview and basic concepts
高中物理:力、物体和平衡
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
TP configuring multiple databases
中信证券网上开户安全吗 开户收费吗
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Vscode modification indentation failed, indent four spaces as soon as it is saved
Summary of tx.origin security issues