当前位置:网站首页>【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;
}
}
边栏推荐
- 超标量处理器设计 姚永斌 第5章 指令集体系 摘录
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- Pytoch deep learning environment construction
- 智捷云——元宇宙综合解决方案服务商
- Ble HCI flow control mechanism
- Using win10 scheduling task program to automatically run jar package at fixed time
- Implementation of super large-scale warehouse clusters in large commercial banks
- Easy to use map visualization
- NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
猜你喜欢
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
估值900亿,超级芯片IPO来了
leetcode:421. The maximum XOR value of two numbers in the array
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
The Block:USDD增长势头强劲
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
随机推荐
第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
[system analyst's road] Chapter 7 double disk system design (structured development method)
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
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
Performance test of Gatling
高中物理:力、物体和平衡
La 18e Conférence internationale de l'IET sur le transport d'électricité en courant alternatif et en courant continu (acdc2022) s'est tenue avec succès en ligne.
【Proteus仿真】基于VSM 串口printf调试输出示例
[proteus simulation] printf debugging output example based on VSM serial port
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
Using win10 scheduling task program to automatically run jar package at fixed time
MVC mode and three-tier architecture
curl 命令妙用
【HCIA持续更新】WLAN概述与基本概念
shell脚本的替换功能实现
【每日一题】871. 最低加油次数
R language plot visualization: plot visualization of multiple variable violin plot in R with plot
What grade does Anxin securities belong to? Is it safe to open an account
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
To sort out messy header files, I use include what you use