当前位置:网站首页>[leetcode] 32. Longest valid bracket
[leetcode] 32. Longest valid bracket
2022-07-26 01:58:00 【Xiaoqu classmate】
32、 Longest valid bracket
subject :
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 * 10^4
s[i] by ‘(’ or ‘)’
Their thinking :
For this bracket matching problem , Generally, stack is used
Let's first find all the index numbers that can match , Then find the longest continuous sequence !
for example :s = )(()()), We can find... With a stack ,
Location 2 And location 3 matching ,
Location 4 And location 5 matching ,
Location 1 And location 6 matching ,
This array is :2,3,4,5,1,6 This is found through the stack , We sort in ascending order !1,2,3,4,5,6
Find out that the length of the longest continuous sequence of the array is the longest effective bracket length !
So the time complexity comes from sorting :O(nlogn).
Next, let's think , Whether the sorting process can be omitted , When playing the stack ?
Look directly at the code and understand ! So the time complexity is :O(n).
class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) return 0;
Deque<Integer> stack = new ArrayDeque<>();
stack.push(-1);
//System.out.println(stack);
int res = 0;
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 {
res = Math.max(res, i - stack.peek());
}
}
}
return res;
}
}

边栏推荐
- Leetcode algorithm 147. insert and sort the linked list
- win下搭建嵌入式开发环境及frp穿透
- 餐饮连锁门店重塑增长背后的数字化转型
- 我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
- What is cross site scripting (XSS)?
- SVN版本控制分支、合并功能使用
- The import and Export button of Damon database table is gray, and the DMP file cannot be imported
- 重发布基础与配置
- When everything can be metauniverse, the development of metauniverse seems to have entered a new stage of development
猜你喜欢

E. Split into two sets

2022 love analysis ― bank digitalization practice report

Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)

【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!

Three modes of CPU

3、 Pinda general permission system__ pd-tools-swagger2

What is cross site scripting (XSS)?

D. Rating compression (thinking + double pointer)
![[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]](/img/fe/746ecaf4123072cca59d7510e9796c.png)
[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]

怎么使用宝塔面板把node全栈项目部署到服务器上
随机推荐
【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
餐饮连锁门店重塑增长背后的数字化转型
Zhinai buys melons (DP backpack)
Analysis of zeromq
Overview of database stress testing methods
FFT用于估计插值后的图像重采样因子
Leetcode/ numbers that appear only once
The e-commerce project is written in the resume. How to answer it during the interview
pdf. JS introduction
【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】
How to display numbers / English time in Excel
大佬们, flinksql datahub源表,源表有字段 timestamp 16位, 写入Ora
SQL injection tutorial: learn through examples
达梦数据库表导入导出按钮灰色,导入不了dmp文件
SQL手工盲注、报错注入
C# 迭代器的实现
Remember a laravel problem script @php artist package:discover handling the post autoload dump event returned with
软件加群验证
Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes