当前位置:网站首页>括号的最大嵌套深度
括号的最大嵌套深度
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;
}
};
边栏推荐
- Project time zone problem solving
- Programming implementation of eight queens
- Leetcode- > dichotomy (III)
- Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)
- 使用redis c库,异步内存泄露的问题
- 真正意义上的数字零售应当具有更加丰富的内涵和意义
- ApacheCon Asia 预热直播之孵化器主题全回顾
- 【SemiDrive源码分析】【驱动BringUp】41 - LCM 驱动 backlight 背光控制原理分析
- C # using sqlsugar updatable system to report invalid numbers, how to solve it? Ask for guidance!
- 路由策略第一关
猜你喜欢
![Abstract intelligent extraction [based on Bert technology]](/img/1c/7c1b0e9bc9af62308f4124104f6110.png)
Abstract intelligent extraction [based on Bert technology]

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

First pass of routing strategy

452页13万字现代智慧乡镇雪亮工程整体解决方案2022版

Parallel desktop startup virtual machine "operation failed" problem solution
![[Yugong series] July 2022 go teaching course 018 switch of branch structure](/img/50/171a083713597f1b5643835377a12d.png)
[Yugong series] July 2022 go teaching course 018 switch of branch structure

路由策略第一关

Plato farm brings a new experience to community users through the LAAS protocol elephant swap

什么是动画效果?什么是过渡效果?

Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
随机推荐
暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)
Kotlin中lateinit和lazy的原理区别是什么
A. YES or YES?
Redis (IX) - redis distributed lock
电商系统结合商品秒杀活动,VR全景不断带来收益
Specific use of multithreading
The 100th of the commercial anti counterfeiting series - boring systems and management processes can really be thrown into the trash can - by the way, analyze a dozen useless unity game self-test proj
Plato farm is expected to further expand its ecosystem through elephant swap
Detailed analysis of trajectory generation tool in psins toolbox
flink cdc 到MySQL8没问题,到MySQL5读有问题,怎么办?
零基础小白也能懂的 Redis 数据库,手把手教你易学易用!
Day 27 of leetcode
Subject 3: Jinan Zhangqiu line 2
「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣
2022年危险化学品经营单位主要负责人复训题库及答案
Leetcode- > 2-point search and clock in (3)
Implementation of API short message gateway based on golang
Using redis C library, the problem of asynchronous memory leakage
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
Maximum subarray cumulative sum less than or equal to K