#include <iostream> #include<stack> using namespace std; class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { stack<char>a,b,c,e; int l=s.length();string ss[l+2];int d=0; for (int i=0;i<=l-1;++i){ a.push(s[i]);ss[++d]=s[i]; } int n=a.size(); for (int i=1;i<=d-1;++i){ if (ss[i][0]=='['){ if (ss[i+1][0]==')'||ss[i+1][0]=='}'){ return false; } } else if (ss[i][0]=='('){ if (ss[i][0]==']'||ss[i+1][0]=='}'){ return false; } } else if (ss[i][0]=='{'){ if (ss[i][0]==')'||ss[i+1][0]==']'){ return false; } } } for (int i=0;i<=n-1;++i){ if (a.top()=='['||a.top()==']'){ b.push(a.top());a.pop(); } else if (a.top()=='('||a.top()==')'){ c.push(a.top());a.pop(); } else if (a.top()=='{'||a.top()=='}'){ e.push(a.top());a.pop(); } } int l1=0,l2=0; while (b.size()!=0){ if (b.top()=='[')l1++; else if (b.top()==']')l2++; b.pop(); if (l1<l2)return false; } if (l1!=l2)return false; l1=0,l2=0; while (c.size()!=0){ if (c.top()=='(')l1++; else if (c.top()==')')l2++; c.pop(); if (l1<l2)return false; } if (l1!=l2)return false; l1=0,l2=0; while (e.size()!=0){ if (e.top()=='{')l1++; else if (e.top()=='}')l2++; e.pop(); if (l1<l2)return false; } if (l1!=l2)return false; return true; } };