题目:


- ### 这道题主要考察stack
#include<bits/stdc++.h>
using namespace std;
const int N=105;
stack<char> stk;
char s[N];
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
cin>>s+1;
bool ans=true;
for(int i=1;i<=n;++i){
//是左括号入栈
if(s[i]=='('){
stk.push(s[i]);
}
//右括号
else{
//stk中有东西且栈首为左括号就出栈
if(stk.size()&&stk.top()=='('){
stk.pop();
}
//否则不对
else{
ans=false;
}
}
}
//都消完了栈里还有左括号也不对
if(stk.size()){
ans=false;
}
cout<<(ans?"Yes":"No")<<"\n";
return 0;
}
整体思路:
- 左括号与右括号完美相消,遇到左括号入栈,遇到右括号出栈
- 遍历后判断栈中还有没有字符















![[香橙派]orange pi zero 3 烧录Ubuntu系统镜像——无需HDMI数据线安装](https://img-blog.csdnimg.cn/direct/7328c4cafd7b49a49ef45bf0440139ae.png)



