考研 408 × 求职面试
知识点 · 数据结构 · 进阶
每日一题 · 数据结构
解析
面试小贴士 · 数据结构
代码实现 · C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// 括号匹配:左括号入栈,右括号校验栈顶
bool check(const string &s){
stack<char> st;
for(char c:s){
if(c=='('||c=='['||c=='{') st.push(c);
else if(c==')'||c==']'||c=='}'){
if(st.empty()) return false;
char t=st.top(); st.pop();
if((c==')'&&t!='(')||(c==']'&&t!='[')||(c=='}'&&t!='{')) return false;
}
}
return st.empty();
}
int main(){
string s; cin>>s;
cout<<(check(s)?"Yes":"No")<<endl;
return 0;
}