class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here // 建立hash映射每个括号的值 stack<char> a; map<char,int> mp; mp['('] = 1; mp[')'] = 4; mp['['] = 2; mp[']'] = 5; mp['{'] = 3; mp['}'] = 6; for(int i = 0; i < s.length(); i++) { auto str = s[i]; if(mp[str] >= 1 && mp[str] <= 3) a.push(str); else { if(a.empty()) return false; auto t = a.top(); if(mp[str] - mp[t] != 3) return false; a.pop(); } } if(a.size()) return false; return true; } };