Topic
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.
Analysis
The use of stacks is relatively simple.
Code
Class Solution {Public:bool IsValid (string s) {if (s.length () = = 0) return true; Stack<int> STA; for (int i = 0; i < s.length (); i++) {if (s[i] = = ' (' | | s[i] = = ' [' | | s[i] = = ' {') St A.push (S[i]); else if (s[i] = = ') ') {if (Sta.empty () | | sta.top ()! = ' (') return false; else Sta.pop (); } else if (s[i] = = '] ') {if (Sta.empty () | | | sta.top ()! = ' [') retur n false; else Sta.pop (); } else {if (Sta.empty () | | | sta.top ()! = ' {') retur n false; else Sta.pop (); }} if (Sta.empty ()) return true; else return false; }};
Valid parentheses--Problem Solving report