当前位置:网站首页>【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;
}
}
边栏推荐
- [acwing] 58 weeks 4489 Longest subsequence
- 完美融入 Win11 风格,微软全新 OneDrive 客户端抢先看
- R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
- 雨量预警广播自动化数据平台BWII 型广播预警监测仪
- 居家打工年入800多万,一共五份全职工作,他还有时间打游戏
- 动态规划股票问题对比
- Is it safe for Bank of China Securities to open an account online?
- TP configuring multiple databases
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
- 码农版隐秘的角落:作为开发者最讨厌的5件
猜你喜欢
VB cannot access database stocks
wuzhicms代码审计
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
PingCode 性能测试之负载测试实践
第十八届IET交直流輸電國際會議(ACDC2022)於線上成功舉辦
leetcode:421. The maximum XOR value of two numbers in the array
kaili不能输入中文怎么办???
码农版隐秘的角落:作为开发者最讨厌的5件
Talk about seven ways to realize asynchronous programming
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
随机推荐
Talk about seven ways to realize asynchronous programming
Great Wall Securities security does not open a securities account
Hidden corners of coder Edition: five things that developers hate most
How to implement a delay queue?
Solution of commercial supply chain coordination system in the mineral industry: build a digital intelligent supply chain platform to ensure the safe supply of mineral resources
【华为HCIA持续更新】SDN与FVC
Zhijieyun - meta universe comprehensive solution service provider
Flask 轻量web框架
第十八届IET交直流输电国际会议(ACDC2022)于线上成功举办
将Opencv绘制图片显示在MFC Picture Control控件上
[acwing] 58 weeks 4490 dyeing
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
设置窗体透明 隐藏任务栏 与全屏显示
leetcode:421. The maximum XOR value of two numbers in the array
利用win10计划任务程序定时自动运行jar包
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
PingCode 性能测试之负载测试实践
wuzhicms代码审计
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
整理混乱的头文件,我用include what you use