当前位置:网站首页>Maximum nesting depth of parentheses
Maximum nesting depth of parentheses
2022-07-27 04:08:00 【Sharp blade CC】
1614. Maximum nesting depth of parentheses
The difficulty is simple 105
If the string meets one of the following conditions , It can be called Valid bracket string **(valid parentheses string**, I could just write it as VPS):
- The string is an empty string
"", Or a not for"("or")"The single character of . - The string can be written as
AB(AAndBString connection ), amongAandBAll are Valid bracket string . - The string can be written as
(A), amongAIt's a Valid bracket string .
Similarly , You can define any valid parenthesis string S Of Nesting depth depth(S):
depth("") = 0depth(C) = 0, amongCIs a single character string , And the character is not"("perhaps")"depth(A + B) = max(depth(A), depth(B)), amongAandBAll are Valid bracket stringdepth("(" + A + ")") = 1 + depth(A), amongAIt's a Valid bracket string
for example :""、"()()"、"()(()())" All are Valid bracket string ( The nesting depth is 0、1、2), and ")(" 、"(()" Are not Valid bracket string .
To give you one Valid bracket string s, Returns the of the string s Nesting depth .
Example 1:
Input :s = "(1+(2*3)+((8)/4))+1"
Output :3
explain : Numbers 8 In the nested 3 Layer in parentheses .
Example 2:
Input :s = "(1)+((2))+(((3)))"
Output :3
Tips :
1 <= s.length <= 100sBy digital0-9And character'+'、'-'、'*'、'/'、'('、')'form- Title Data guarantee bracket expression
syes Valid parenthesis expression
Ideas :
For bracketed calculation questions , We can often think with stacks .
Traversal string ss, If you encounter an open parenthesis , Then put it on the stack ; If you encounter a right parenthesis , Then pop up the left bracket at the top of the stack , Match the closing bracket . The maximum stack size in this process , That is to say ss The nesting depth of .
When the code is implemented , Because we only need to consider the stack size , We can use a variable size Indicates the size of the stack , When you encounter an open parenthesis, add one , When you encounter a right parenthesis, subtract it by one , Thus, it indicates the change of elements in the stack . In the process size The maximum value of is ss The nesting depth of .
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;
}
};
边栏推荐
- Notes are a little fun
- leetcode每日一题:数组的相对排序
- Golang JWT cross domain authentication
- 科目三: 济南章丘五号线
- 商业打假系列之第一百之--无聊的制度和管理流程真的可以扔进垃圾桶-顺便分析十几个无用的Unity游戏自检项目
- 452页13万字现代智慧乡镇雪亮工程整体解决方案2022版
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- leetcode:433. 最小基因变化
- 利用LCD1602显示超声波测距
- 222. 完全二叉树的节点个数
猜你喜欢

线上一个隐匿 Bug 的复盘

《MySQL》认识MySQL与计算机基础知识

Framework学习之旅:init 进程启动过程

暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)

3381. Mobile keyboard (Tsinghua University postgraduate entrance examination machine test question)

Alibaba cloud server domain name and port web page cannot access the problem record

"Gonna be right" digital collection is now on sale! Feel the spiritual resonance of artists

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

Subject 3: Jinan Zhangqiu line 2

NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
随机推荐
Golang sends email to the mail Library
Case when in MySQL returns multiple field processing schemes
Parallels Desktop启动虚拟机“操作失败”问题解决
ApacheCon Asia 预热直播之孵化器主题全回顾
【MySQL系列】MySQL索引事务
VR全景制作在家装行业是谈单利器?这是为什么呢?
Subject 3: Jinan Zhangqiu line 3
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
使用redis c库,异步内存泄露的问题
基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)
整理字符串
NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
Bean Validation原理篇--07
Detailed analysis of trajectory generation tool in psins toolbox
括号的最大嵌套深度
Have you encountered the situation that CDC reads incomplete MySQL fields? How to deal with it?
【obs】动态码率:码率估算
Parallel desktop startup virtual machine "operation failed" problem solution
js实现页面跳转与参数获取加载
0726~ resume sorting interview summary