当前位置:网站首页>括号的最大嵌套深度
括号的最大嵌套深度
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;
}
};
边栏推荐
- A. Parkway Walk
- 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
- Parallels Desktop启动虚拟机“操作失败”问题解决
- 知识图谱:知识表示
- 11.zuul路由网关
- 酷雷曼VR全景为你铺设创业之路
- Chapter 4 决策树和随机森林
- Chapter 5 decision tree and random forest practice
- H. 265 web player easyplayer's method of opening video to the public
- 注释有点好玩哦
猜你喜欢

Apachecon Asia preheating live broadcast incubator theme full review

基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

电商系统结合商品秒杀活动,VR全景不断带来收益

An online duplicate of a hidden bug

Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China

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

3381. 手机键盘(清华大学考研机试题)

C. Cypher

分享当下人生——一个高中毕业生在中央电视台的六星期实习经历
随机推荐
288 page 180000 word intelligent campus overall design directory
Chapter 5 decision tree and random forest practice
04. Detailed steps for installing the simulated browser chromedriver in Google browser
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
222. 完全二叉树的节点个数
PSINS工具箱中轨迹生成工具详细解析
知识图谱:知识表示
3381. 手机键盘(清华大学考研机试题)
Using redis C library, the problem of asynchronous memory leakage
Skywalking distributed system application performance monitoring tool - medium
一维数组的应用
科目三: 济南章丘三号线
开机启动流程及营救模式
leetcode:433. 最小基因变化
452页13万字现代智慧乡镇雪亮工程整体解决方案2022版
Programming implementation of eight queens
Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
C. Cypher
B. ICPC Balloons