1 条题解
-
0
用一个变量记录当前栈内有多少个‘ ( ’,每次碰到 ' ) ' 时直接--,如果不够就输出 NO 然后 return 0,如果最后还有剩余的' ( '依然输出 NO,否则输出 YES。
#include<bits/stdc++.h> using namespace std; int main() { string s;int cnt=0; cin>>s; for(int i=0;i<s.length();i++){ if(s[i]=='(')cnt++; if(s[i]==')'){ if(cnt<1){ cout<<"NO"; return 0; }else cnt--; } } if(!cnt)cout<<"YES"; else cout<<"NO"; return 0; }
- 1
信息
- ID
- 701
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 66
- 已通过
- 20
- 上传者
京公网安备 11011102002149号