Given A string containing just the characters,,, ‘(‘ ‘)‘ , and ‘{‘ ‘}‘ ‘[‘ ‘]‘ , determine if the input string I S valid.
The brackets must close in the correct order, and is all valid but and is not "()" "()[]{}" "(]" "([)]" .
#include <iostream> #include <string> #include <stack> #include <map>using namespace Std;class Solution {private:map<char,char> pairs;stack<int> pstack;public:solution () {pairs[') '] = ' (';p airs['} ') = ' {';p airs['] = ' [';} BOOL IsValid (string s) {int len = s.length (); if (len%2) return false; int i = 0;map<char,char>::iterator It;char sen;for (i = 0; i < len; i++) {it = Pairs.find (S[i]); if (it = = Pairs.end ( ) {Pstack.push (s[i]); continue;} Sen = It->second;if (!pstack.empty ()) {char tmp = pstack.top (); if (tmp = = sen) {Pstack.pop ();} Else{return false;}} Else{return false;}} if (Pstack.empty ()) return True;return false; }}; int main () {Solution S = solution (); cout << s.isvalid ("[{}]{{} ()}") << Endl;return 0;}
[Leetcode] Valid parentheses