题目
1614.括号的最大嵌套深度
题目大意
![img](https://i-blog.csdnimg.cn/blog_migrate/ee13558091cd27fdba571169dc6bbd29.png)
思路
要求就是考虑字符串中字符的最深的嵌套深度,实际上就是遇到一个’(’,当前深度+1,遇到一个’)’ ,深度-1,而答案每次需要根据当前深度进行更新,取最大的深度。
代码
class Solution {
public:
int maxDepth(string s) {
int len=s.length(),now=0,ans=0;
for(int i=0;i<len;i++){
if(s[i]=='(')now++;
else if(s[i]==')')now--;
ans=max(ans,now);
}
return ans;
}
};