题解 | 括号匹配深度

括号匹配深度

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

#include<iostream>
#include<stack>
#include<string>
#include<algorithm> 
using namespace std;

int main() {
	string s;
	cin >> s;
	size_t ans = 0;
	stack<char> stack;
	for (int i = 0; i < s.size(); i++) {
		char c = s[i];
		if (c == '(') {
			stack.push(c);
			ans = max(stack.size(), ans);
		}
		else if (!stack.empty() && stack.top() == '(') stack.pop();
	}
	cout << ans; 
}

//直接用stack,感觉dfs(n, k)也是类似于栈

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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