当前位置:网站首页>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;
边栏推荐
- Redis Foundation
- Using the sap ui5 cli command line tool to build and run SAP ui5 applications
- How to Load Data from CSV (Data Preparation Part)
- Comprehensive RTL code design method and precautions
- Regular check matches positive integer or decimal limit between [0-100] and [0-1000]
- 关于斜率优化
- [advanced C language] integer storage in memory
- AC自动机
- Leetcode 797. All possible paths
- flutter系列之:flutter中常用的container layout详解
猜你喜欢

ORA-04098: trigger ‘xxx.xxx‘ is invalid and failed re-validation

LeetCode-32-最长有效括号

LeetCode-43-字符串相乘

【 C Advanced language】 Integer Storage in Memory

【C語言進階】整型在內存中的存儲

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

How to manually drag nodes in the Obsidian relationship graph

Answer fans' questions | count the number and frequency of letters in the text

Obsidian关系图谱如何让节点可以手动拖动

JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
随机推荐
LeetCode-98-验证二叉搜索树
Iros 2021 | new idea of laser vision fusion? Lidar intensity diagram +vpr
Analysis on the development history and market development status of China's system integration industry in 2020 [figure]
Hangzhou Electric Zhongchao 91006 guess the weight
LeetCode-104-二叉树的最大深度
[Part 16] copyonwritearraylist source code analysis and application details [key]
Codeforces Round #739 (Div. 3)解题报告
Codeworks round 739 (Div. 3) problem solving Report
2022年6月9日 16:29:41 日记
JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
JVM object allocation policy TLAB
select _ Lazy loading
Leetcode-76- minimum covering substring
【生活思考】文字与语音
RANSAC提取平面(MATLAB内置函数)
A collection of commonly used open source data sets for face recognition
Cs144 lab0 lab1 record
實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
[Game Theory - introduction]
数据库每日一题---第9天:销售员