题解 | #括号字符串的有效性#
括号字符串的有效性
http://www.nowcoder.com/practice/769abd985f3849d0b9415b082f94c192
//简单的题简单做
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
cin>>str;
int res=0;
for(int i=0;i<str.size();i++){
if(str[i]=='(')
res++;
else if(str[i]==')')
{
res--;
if(res<0){
cout<<"NO\n";
return 0;
}
}
else{
cout<<"NO\n";
return 0;
}
}
if(res==0)
cout<<"YES\n";
else cout<<"NO\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
cin>>str;
int res=0;
for(int i=0;i<str.size();i++){
if(str[i]=='(')
res++;
else if(str[i]==')')
{
res--;
if(res<0){
cout<<"NO\n";
return 0;
}
}
else{
cout<<"NO\n";
return 0;
}
}
if(res==0)
cout<<"YES\n";
else cout<<"NO\n";
return 0;
}