输入:
仅包含{,},(,),[,]的字符串
输出:
如果括号匹配输出:YES
否则输出:NO
Solution:
#include<iostream>
#include<string>
#include<stack>
using namespace std;bool check(const string&);int main(){string input;cin>>input;if(check(input)){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}return 0;
} bool check(const string & input){stack<char> temp_stack;for(int i=0; i<input.size(); i++){if(input[i]=='('||input[i]=='{'||input[i]=='['){temp_stack.push(input[i]);}else if(input[i]==')'){if(temp_stack.top()=='(')temp_stack.pop();elsereturn false;}else if(input[i]=='}'){if(temp_stack.top()=='{')temp_stack.pop();elsereturn false;}else if(input[i]==']'){if(temp_stack.top()=='['){temp_stack.pop();}elsereturn false;}}if(temp_stack.empty()){return true;}else{return false;}
}
思路:用栈的思想