辅助栈
代码:
1 bool isValid(string s) { 2 stack<char> st; 3 4 for (auto c : s) { 5 if (st.empty()) 6 st.push(c); 7 else if (c == ‘)‘) { 8 if (st.top() != ‘(‘) 9 return false; 10 st.pop(); 11 } 12 else if (c == ‘]‘) { 13 if (st.top() != ‘[‘) 14 return false; 15 st.pop(); 16 } 17 else if (c == ‘}‘) { 18 if (st.top() != ‘{‘) 19 return false; 20 st.pop(); 21 } 22 else 23 st.push(c); 24 } 25 26 return st.empty(); 27 }
原文:http://www.cnblogs.com/boring09/p/4268518.html