当前位置:网站首页>括号的最大嵌套深度
括号的最大嵌套深度
2022-07-27 02:50:00 【利刃Cc】
1614. 括号的最大嵌套深度
难度简单105
如果字符串满足以下条件之一,则可以称之为 有效括号字符串**(valid parentheses string**,可以简写为 VPS):
- 字符串是一个空字符串
"",或者是一个不为"("或")"的单字符。 - 字符串可以写为
AB(A与B字符串连接),其中A和B都是 有效括号字符串 。 - 字符串可以写为
(A),其中A是一个 有效括号字符串 。
类似地,可以定义任何有效括号字符串 S 的 嵌套深度depth(S):
depth("") = 0depth(C) = 0,其中C是单个字符的字符串,且该字符不是"("或者")"depth(A + B) = max(depth(A), depth(B)),其中A和B都是 有效括号字符串depth("(" + A + ")") = 1 + depth(A),其中A是一个 有效括号字符串
例如:""、"()()"、"()(()())" 都是 有效括号字符串(嵌套深度分别为 0、1、2),而 ")(" 、"(()" 都不是 有效括号字符串 。
给你一个 有效括号字符串s,返回该字符串的 s嵌套深度 。
示例 1:
输入:s = "(1+(2*3)+((8)/4))+1"
输出:3
解释:数字 8 在嵌套的 3 层括号中。
示例 2:
输入:s = "(1)+((2))+(((3)))"
输出:3
提示:
1 <= s.length <= 100s由数字0-9和字符'+'、'-'、'*'、'/'、'('、')'组成- 题目数据保证括号表达式
s是 有效的括号表达式
思路:
对于括号计算类题目,我们往往可以用栈来思考。
遍历字符串 ss,如果遇到了一个左括号,那么就将其入栈;如果遇到了一个右括号,那么就弹出栈顶的左括号,与该右括号匹配。这一过程中的栈的大小的最大值,即为 ss 的嵌套深度。
代码实现时,由于我们只需要考虑栈的大小,我们可以用一个变量 size 表示栈的大小,当遇到左括号时就将其加一,遇到右括号时就将其减一,从而表示栈中元素的变化。这一过程中 size 的最大值即为 ss 的嵌套深度。
class Solution {
public:
int maxDepth(string s) {
int tmp = 0;
int size = 0;
int n = s.size();
for(int i = 0; i < n; ++i)
{
if(s[i] == '(')
{
size++;
}
else if(s[i] == ')')
{
size--;
}
tmp = max(tmp, size);
}
return tmp;
}
};
边栏推荐
- [untitled]
- C language introduction practice (12): find the value of natural constant e
- 开机启动流程及营救模式
- 阿里云服务器域名加端口网页不能访问问题记录
- flinkSQLclient创建的job,flink重启就没了,有什么办法吗?
- Alibaba cloud server domain name and port web page cannot access the problem record
- Kotlin中lateinit和lazy的原理区别是什么
- JVM原理简介
- Implementation of API short message gateway based on golang
- 函数指针与回调函数
猜你喜欢
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)

STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)

Maximum subarray cumulative sum less than or equal to K

路由策略第一关

11. Zuul routing gateway

Subject 3: Jinan Zhangqiu line 3

2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作

Parallel desktop startup virtual machine "operation failed" problem solution

C. Cypher
随机推荐
Is it safe for tongdaxin to open an account
Apachecon Asia preheating live broadcast incubator theme full review
Plato farm is expected to further expand its ecosystem through elephant swap
288 page 180000 word intelligent campus overall design directory
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
在Golang结构体中使用tag标签
On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!
A. YES or YES?
C#怎么实现给Word每一页设置不同文字水印
The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)
Day 28 of leetcode
科目三: 济南章丘6号线
3381. Mobile keyboard (Tsinghua University postgraduate entrance examination machine test question)
C language introduction practice (12): find the value of natural constant e
JVM原理简介
flink cdc 到MySQL8没问题,到MySQL5读有问题,怎么办?
路由策略第一关
【obs】动态码率:码率估算
Notes are a little fun
电商系统结合商品秒杀活动,VR全景不断带来收益