当前位置:网站首页>Leetcode-32- longest valid bracket
Leetcode-32- longest valid bracket
2022-06-11 21:28:00 【z754916067】
subject

Ideas
- I think we should start with the stack , Anyway, matching parentheses are stacks .
- Problem solving is mainly to put the index on the stack , Calculate the length of the longest valid bracket according to the index , A lot of sense .
Code
Stack<Integer> st1 = new Stack<>();
// Length of the longest substring
int max = 0;
//( Starting position
int start=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
st1.push(i);
}
else{
// Meet immediately ) 了 But the stack is empty
if(st1.empty()){
start+=1;
}
else{
// At this time, there is ( It can pop up If it is empty after pop-up All matching strings are valid , The length is i-start+1
st1.pop();
if (st1.isEmpty()) {
max = Math.max(max,i-start+1);
}else {
// If it's not empty Explain to st1 Up to the top index of All parentheses are valid strings The length is i-st1.peek()
max = Math.max(max,i-st1.peek());
}
}
}
}
return max;
边栏推荐
- RANSAC提取圆柱(MATLAB内置函数)
- 12 golden rules of growth
- Analysis on the development history and market development status of China's system integration industry in 2020 [figure]
- js对返回的数据的各种数据类型进行非空判断。
- Leetcode 797. All possible paths
- Chain storage structure of linear table
- Database daily question --- day 9: salesperson
- LeetCode-32-最长有效括号
- Syntax of SQL
- 八、BOM - 章节课后练习题及答案
猜你喜欢
随机推荐
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Tensorflow 2. X Getting Started tutorial
JS performs non empty judgment on various data types of the returned data.
杭电中超9 1006 Guess the Weight
如何使用 SAP Kyma 控制台手动发送 SAP Commerce Cloud Mock 应用暴露的事件
Codeforces Round #740 Div. 2 解题报告
LabVIEW Arduino electronic weighing system (project Part-1)
八、BOM - 章节课后练习题及答案
[Part 14] source code analysis and application details of completionservice class [key]
Supplementary questions for the training ground on September 11, 2021
Game client performance (memory) [previous]
Educational Codeforces Round 114 (Rated for Div. 2) D
Redis basic data type (list)
Redis data type (string)
Realize the same length of tablayout subscript and text, and change the selected font size
Database daily question --- day 9: salesperson
Redis Foundation
select _ Lazy loading
Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
使用 SAP UI5 CLI 命令行工具构建和运行 SAP UI5 应用
![BZOJ3189 : [Coci2011] Slika](/img/46/c3aa54b7b3e7dfba75a7413dfd5b68.png)






![Game client performance (memory) [previous]](/img/b6/869b83e92efcdf95aa6bd5e8ff0d10.jpg)
