当前位置:网站首页>【LeetCode】32、 最长有效括号
【LeetCode】32、 最长有效括号
2022-07-26 01:53:00 【小曲同学呀】
32、 最长有效括号
题目:
给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
示例1:
输入:s = "(()"
输出:2
解释:最长有效括号子串是 "()"
示例2:
输入:s = ")()())"
输出:4
解释:最长有效括号子串是 "()()"
示例3:
输入:s = ""
输出:0
提示:
0 <= s.length <= 3 * 10^4
s[i] 为 ‘(’ 或 ‘)’
解题思路:
对于这种括号匹配问题,一般都是使用栈
我们先找到所有可以匹配的索引号,然后找出最长连续数列!
例如:s = )(()()),我们用栈可以找到,
位置 2 和位置 3 匹配,
位置 4 和位置 5 匹配,
位置 1 和位置 6 匹配,
这个数组为:2,3,4,5,1,6 这是通过栈找到的,我们按递增排序!1,2,3,4,5,6
找出该数组的最长连续数列的长度就是最长有效括号长度!
所以时间复杂度来自排序:O(nlogn)。
接下来我们思考,是否可以省略排序的过程,在弹栈时候进行操作呢?
直接看代码理解!所以时间复杂度为: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;
}
}

边栏推荐
- Make and makefile summary I
- Phoenix中常用shell操作
- SVN版本控制分支、合并功能使用
- Recommend a super good UI automation tool: uiautomator2!
- Worthington产气荚膜梭菌神经氨酸酶的特征及测定
- QTreeWidget虚线设置
- Implementation of recommendation system collaborative filtering in spark
- “蔚来杯“2022牛客暑期多校训练营2 D.[Link with Game Glitch] 二分答案+SPFA判环
- There is no setter method in grpc list under flutter. How to use related attributes
- Infinite fraction path (BFS pruning)
猜你喜欢

pdf. JS introduction
![Niuke - bm39 serialized binary tree [hard]](/img/c4/f14fe8488bbf28689fa3f02cdf4dae.png)
Niuke - bm39 serialized binary tree [hard]

言语理解-片段阅读的结构剖析练习
![[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]
![[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!](/img/52/8c1520db38ffa8927e975b6f244a65.png)
[independent station construction] Shopify seller: learn these points and double the sales volume of online stores!

pt-onnx-ncnn转换的问题记录(接yolov5训练)

Qt程序美化之样式表的使用方法,Qt使用图片作为背景与控件透明化,Qt自定义按钮样式

C# 迭代器的实现

Protect syslog servers and devices

# Dest0g3 520迎新赛(更新中)
随机推荐
“蔚来杯“2022牛客暑期多校训练营2 个人题解集合
How to do Taobao live broadcast and how to do the anchor to drain and sell products
Digital transformation behind the reshaping growth of catering chain stores
HTC手机官解、S-ON/S-OFF与超级CID的关系
There is no setter method in grpc list under flutter. How to use related attributes
E. Split into two sets
“蔚来杯“2022牛客暑期多校训练营2 K.[Link with Bracket Sequence I] 括号序列 DP
SQLyog数据导入导出图文教程
BGP knowledge points summary
IP address of the network
How does Flink SQL configure to print the insert parameter log
Codisvsrediscluster: which cluster scheme should I choose?
"Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section
[Verilog digital system design (Xia Yuwen) 3 ----- basic concepts of Verilog syntax 1]
Worthington产气荚膜梭菌神经氨酸酶的特征及测定
Shell exercises
“蔚来杯“2022牛客暑期多校训练营2 D.[Link with Game Glitch] 二分答案+SPFA判环
Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
Huawei wireless device WDS configuration command
【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】