当前位置:网站首页>【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;
}
}
边栏推荐
- 创业两年,一家小VC的自我反思
- 就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- Set the transparent hidden taskbar and full screen display of the form
- [test development] software testing - Basics
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
- 利用win10计划任务程序定时自动运行jar包
- Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
- 解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
猜你喜欢
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
【Hot100】32. 最长有效括号
Zhijieyun - meta universe comprehensive solution service provider
整理混乱的头文件,我用include what you use
[HCIA continuous update] WLAN overview and basic concepts
【测试开发】软件测试——基础篇
上市公司改名,科学还是玄学?
码农版隐秘的角落:作为开发者最讨厌的5件
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
[Huawei HCIA continuous update] SDN and FVC
随机推荐
一加10 Pro和iPhone 13怎么选?
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
Cocoscreator event dispatch use
ARTS_20220628
To sort out messy header files, I use include what you use
About the pit of firewall opening 8848 when Nacos is started
Face_recognition人脸识别之考勤统计
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
【HCIA持续更新】WLAN概述与基本概念
一文掌握数仓中auto analyze的使用
Datakit -- the real unified observability agent
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
【Hot100】31. 下一个排列
gatling 之性能测试
缓存穿透、缓存击穿、缓存雪崩分别是什么
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
KS007基于JSP实现人个人博客系统