当前位置:网站首页>【Hot100】32. Longest valid bracket
【Hot100】32. Longest valid bracket
2022-07-04 17:48:00 【Wang Liuliu's it daily】

Reference resources :https://leetcode.cn/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode-solution/
Reference resources :leetcode hot 100-32. Longest valid bracket
- Dynamic programming
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;
}
}
- Stack
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;
}
}
- No need for extra space
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;
}
}

边栏推荐
- 【Hot100】32. 最长有效括号
- 曾经的“彩电大王”,退市前卖猪肉
- Win32 API access route encrypted web pages
- Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
- 数学分析_笔记_第7章:多元函数的微分学
- 要上市的威马,依然给不了百度信心
- 一文掌握数仓中auto analyze的使用
- 使用3DMAX制作一枚手雷
- 离线、开源版的 Notion—— 笔记软件Anytype 综合评测
- Easy to use map visualization
猜你喜欢

整理混乱的头文件,我用include what you use

一加10 Pro和iPhone 13怎么选?

【HCIA持续更新】WLAN概述与基本概念

Implementation of super large-scale warehouse clusters in large commercial banks

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

上市公司改名,科学还是玄学?

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

要上市的威马,依然给不了百度信心

VSCode修改缩进不成功,一保存就缩进四个空格

Vb无法访问数据库stocks
随机推荐
【每日一题】556. 下一个更大元素 III
设置窗体透明 隐藏任务栏 与全屏显示
Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
新享科技发布小程序UniPro小优 满足客户移动办公场景
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
我写了一份初学者的学习实践教程!
Is it safe for Great Wall Securities to open an account? How to open a securities account
It's too convenient. You can complete the code release and approval by nailing it!
Two methods of MD5 encryption
【HCIA持续更新】WLAN工作流程概述
整理混乱的头文件,我用include what you use
整理混乱的头文件,我用include what you use
NFT流动性市场安全问题频发—NFT交易平台Quixotic被黑事件分析
tx.origin安全问题总结
KS007基于JSP实现人个人博客系统
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
上市公司改名,科学还是玄学?
Using win10 scheduling task program to automatically run jar package at fixed time
开发者,MySQL专栏完更,助你轻松从安装到入门进阶