标签:
合法准则即是否成对匹配
(())合法
({])不合法
())(不合法
思路:用栈模拟即可
class Solution {public:bool isValid(string s) {stack<char> stk;for (size_t i = 0; i < s.size(); i++){if (stk.empty())stk.push(s[i]);else if (isPair(stk.top(),s[i])){stk.pop();}else{stk.push(s[i]);}}return stk.empty() ? true : false;}bool isPair(char left, char right){if (left == ‘(‘ && right == ‘)‘)return true;if (left == ‘[‘ && right == ‘]‘)return true;if (left == ‘{‘ && right == ‘}‘)return true;return false;}};
标签:
原文地址:http://www.cnblogs.com/flyjameschen/p/786f7abf9506c02c094f4c492e7fff50.html