题解 | 括号匹配深度

括号匹配深度

https://www.nowcoder.com/practice/a2d5b1875bb0408384278f40d1f236c9

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    string s;
    cin >> s;
    
    int current_depth = 0;  // 当前深度
    int max_depth = 0;      // 最大深度
    
    for (char c : s) {
        if (c == '(') 
		{
            current_depth++;
            // 更新最大深度
            max_depth = max(max_depth, current_depth);
        } else 
		{ 
            current_depth--;
        }
    }
    
    cout << max_depth << endl;
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务