当前位置:网站首页>The longest valid bracket of question 32 in C language. Implement with stack
The longest valid bracket of question 32 in C language. Implement with stack
2022-07-21 18:42:00 【Take care of two dogs and never let them go bad】
Give you one that only contains '(' and ')' String , Find the longest effective ( The format is correct and continuous ) The length of the bracket substring .
Example 1:
Input :s = "(()"
Output :2
explain : The longest valid bracket substring is "()"
Example 2:
Input :s = ")()())"
Output :4
explain : The longest valid bracket substring is "()()"
Example 3:
Input :s = ""
Output :0
Tips :
0 <= s.length <= 3 * 104
s[i] by '(' or ')'
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-valid-parentheses
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Here are two ways to implement with stack :( The mind is the same )
Mode one :
arr The record appears '(' Subscript of time ,map The record appears ')' The effective length of bracket substring
Traverse ‘s’,
yes '(' when ,arr Save the current subscript ‘i’
yes ')' when , Take out arr Last of '(' The corresponding subscript ‘l’, Form valid parentheses , At this time, the effective bracket length is ‘cout=i-l+1’,map[i] by count, You need to consider , If it's continuous , be map[l-1] Valuable ,l yes ‘(’ The subscript that appears ,l-1 It was the last time ‘)’ The subscript that appears , here map[i]=count+map[l-1]
Finally back to Math.max(map[i],max)
Such as :s=")()())"
i=2 when ,arr=[1], count = i-l+1 = 2-1+1, map={2:2}, max=2
i=4 when ,arr=[3], count = 4-3+1, map[3-1] It's worth it 2, map[4]=count+map[2]=2+2,max=4
Square twelve :( Force deduction official method )
Always keep the elements at the bottom of the stack in the currently traversed elements 「 The subscript of the last closing bracket that is not matched 」, This approach mainly considers the treatment of boundary conditions , Other elements in the stack maintain the subscript of the left bracket :
For every one of them ‘(’ , We put its subscript on the stack
For every one of them ‘)’ , First, we pop up the top element of the stack, indicating that it matches the current closing parenthesis :
If the stack is empty , Indicates that the current closing bracket is a closing bracket that has not been matched , We put its subscript on the stack to update what we mentioned earlier 「 The subscript of the last closing bracket that is not matched 」
If the stack is not empty , The subscript of the current right bracket minus the top element of the stack is 「 The length of the longest valid parenthesis ending with the closing parenthesis 」
If you want to know more basic knowledge , You can pay attention to my other blogs related to bracket matching :
There are various classic examples
int longestValidParentheses(char* s) {
int maxans = 0, n = strlen(s);
int stk[n + 1], top = -1;
stk[++top] = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
stk[++top] = i;
} else {
--top;
if (top == -1) {
stk[++top] = i;
} else {
maxans = fmax(maxans, i - stk[top]);
}
}
}
return maxans;
}
边栏推荐
- C language to find the greatest common divisor and the least common multiple of two numbers
- Digital marketing has become a trend for small programs to break the situation
- CY5-PNA花菁染料CY5标记肽核酸PNA的实验要求
- NFT与奢侈品文化的天然契合:NFT满足了人类寻求独特性和地位的天性
- 通用分页(分页代码的封装)
- codeforces每日5题(均1500)-第二十一天
- Tio2-fe3o4/mil-101 (CR) magnetic composite photocatalytic material | nano drug carriers with core-shell structure (siRNA pcnps)
- [IOT design. 3] STM32 single chip microcomputer + smart cloud aiot+ pig house monitoring and system hardware design
- Ctfhub information disclosure
- Skiasharp's WPF self drawn bouncing ball (case version)
猜你喜欢
一种非极大值抑制(non_max_suppression, nms)的代码实现方式
关于GB 2312 编码顺序的研究
Tio2-fe3o4/mil-101 (CR) magnetic composite photocatalytic material | nano drug carriers with core-shell structure (siRNA pcnps)
2022 dsctf first digital space security attack and defense competition
Control in canoe panel: switch/indicator
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
Application of carboxymethyl fluorescein 6-fam modified PNA peptide nucleic acid 6-fam-pna|cy3-pna fluorescent dye Cy3 coupling PNA peptide nucleic acid
丹磺酰荧光素标记肽核酸偶联多肽|Dansyl-Ahx-PNA荧光素标记肽核酸的合成路线
无码时代,企业数字化转型该如何发展?
RENIX_IPv6自动配置——网络测试仪实操
随机推荐
聚乙烯亚胺(PEI)改性MIL-101(Cr)|多酸基金属有机框架材料(POM-MOF)|二茂铁改性MIL-88B|齐岳生物
Vs2017 monitoring window
In the code free era, how should enterprises' digital transformation develop?
Synthesis of tetramethyl rhodamine TRITC modified peptide nucleic acid PNA | TRITC PNA | fluorescein labeled PNA
C language -- 24 Gobang
Metal organic framework mil-100 (CR) and mil-101 (CR) Supported Phosphotungstic Acid | zirconium based metal organic framework [email pr
C语言练习题目+答案:21-25
I have these experiences in changing from military civilian to programmer
Skiasharp's WPF self drawn bouncing ball (case version)
2022 dsctf first digital space security attack and defense competition
Golang collection: custom types and method sets
Metal organic framework mil-101 (CR) loaded chitosan material | mil-101 (CR) @cs | glycyrrhetinic acid (GA) modified metal organic framework material uio-66-nh2 (uio-66- NH2 GA)
【IoT毕设.3】STM32单片机+机智云AIoT+猪舍监测与系统硬件设计
It is not just products that keep pace with the times. Look at FAW Toyota's confidence in "Second Entrepreneurship" from Carola Ruifang
Kv260 (I) running AI box
College student community management system based on SSM framework
c语言入门---操作符
Event object learning
dataframe 绘制相关系数拟合线 散点图拟合线
Binary search principle, template, exercise