1 //思路很清晰,直接用stack 2 class Solution 3 { 4 unordered_map<char,char> hash = {{'(',')'},{'[',']'},{'{','}'}}; 5 public: 6 bool isValid(string s) 7 { 8 stack<char> stk; 9 for(auto a : s) 10 { 11 if(a == '(' || a == '{' || a == '[') stk.push(a); 12 else 13 { 14 if(!stk.empty() && a == hash[stk.top()]) stk.pop(); 15 else return false; 16 } 17 } 18 return stk.size() == 0; 19 } 20 };
来源:https://www.cnblogs.com/yuhong1103/p/12499201.html