题解 | 括号匹配深度

括号匹配深度

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

#include<bits/stdc++.h>
using namespace std;
int main(){
	string str;
	cin>>str;
	if(str.size() == 0){
		cout<<0;
		return 0;
	}
	stack<char> s;
	int count = 0;
	int maxCount = 0;
	for(int i=0;i<str.size();i++){
		char c = str[i];
		if(c == '('){
			s.push(c);
			count++;
			
		}
		if(c == ')'){
			s.pop();
			count--;
		}
		maxCount = max(count,maxCount);
	}
	cout<<maxCount<<endl;
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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