思路:遇到左括号+1,并用当前括号深度更新最大深度,遇到右括号当前深度-1
class Solution {
public:
int maxDepth(string s) {
int maxcnt = 0, cnt = 0;
for (char ach : s) {
if (ach == '(') {
cnt += 1;
if (cnt > maxcnt) {
maxcnt = cnt;
}
}
else if (ach == ')') {
cnt -= 1;
}
}
return maxcnt;
}
};