Valid parentheses
Given A string containing just the characters‘(‘,‘)‘,‘{‘,‘}‘,‘[‘and‘]‘, determine if the input string is valid.
The brackets must close in the correct order,"()"and"()[]{}"is all valid but"(]"and"([)]"is not.
Brackets match, using the stack solution.
Class Solution {public: bool IsValid (string s) { stack<char>a; int n = s.size (); for (int i = 0;i < n;i++) { if (s[i] = = ' (' | | s[i] = = ' {' | | s[i] = = ' [') { a.push (s[i]); } else if (s[i] = = ') ' | | S[i] = = '} ' | | S[i] = = '] ') { if (A.empty ()) return false; if (s[i] = = ') ' && a.top () = = ' (') {a.pop (); Continue } if (s[i] = = '} ' && a.top () = = ' {') {a.pop (); Continue } if (s[i] = = '] ' && a.top () = = ' [') {A.pop (); Continue } return false; } } if (A.empty ()) return true; else return false; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode-20-valid parentheses