1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| bool isValid(string s) { stack<char> par; char tmp; for (int i = 0; i < s.length(); i++) { tmp = s[i]; if(tmp=='('||tmp=='['||tmp=='{') par.push(s[i]); else { if (par.empty()) return false; char top = par.top(); switch (s[i]) { case ')': if (top != '(') return false; break; case ']': if (top != '[') return false; break; case '}': if (top != '{') return false; break; default: return false; } par.pop(); } } if (par.empty()) return true; else return false; }
|