题目
1614. 括号的最大嵌套深度 - 力扣(LeetCode)
Python
class Solution:
def maxDepth(self, s: str) -> int:
count=0
maxCount=0
for i in s:
if i=='(':
count+=1
if i==')':
maxCount=max(maxCount,count)
count-=1
return maxCount
C++
class Solution {
public:
int maxDepth(string s)
{
int count=0,maxCount=0;
for(auto c:s)
{
if(c=='(') count ++;
if(c==')') maxCount=max(maxCount,count--);
}
return maxCount;
}
};
C语言
#define MAX(a,b) ((a)>(b)?(a):(b))
int maxDepth(char* s)
{
int count=0,MaxCount=0;
for(int _=0;_<strlen(s);_++)
{
if(s[_]=='(') count++;
if(s[_]==')')
{
MaxCount=MAX(count,MaxCount);
count--;
}
}
return MaxCount;
}