牛客题解 | 获取n维数组的最大深度
题目
题解
题目难度:简单题目
知识点:字符串、数学逻辑
思路:
用两个变量left和ans。其中当遇到“[”时,left的值增加一,当遇到“]”时,left的值减少一。再这个变化过程中,left能达到的最大值为最大的层数,将其保存再ans中。
#include<iostream>
using namespace std;
int main() {
char ch;
int left = 0;
int ans = 0;
while (cin >> ch) {
if (ch == '[') {
++left;
ans = max(ans, left);
} else if (ch == ']') {
--left;
}
}
cout << ans << endl;
return 0;
}

浙公网安备 33010602011771号