Original title Link: https://oj.leetcode.com/problems/valid-parentheses/
Checks if the sequence of parentheses is valid. The solution here is to maintain a stack, if it is an opening parenthesis, then push to the stack, if it is a closing parenthesis, then check the top of the stack symbol, if the corresponding parentheses, it will pop up. Otherwise, it returns false directly. When the string is scanned to the end, the stack is checked for null, and if it is empty, all parentheses are match.
Class Solution {Public:bool IsValid (string s) {stack<char> stk; int i = 0; while (I < s.size ()) {char c = s[i]; Switch (c) {case ' (': Case ' [': Case ' {'): Stk.push (c); ++i; Break Case ') ': if (!stk.empty () && stk.top () = = ' (') {stk.pop (); ++i; Break } else {return false; } case ': ' If (!stk.empty () && stk.top () = = ' [') {STK.P OP (); ++i; Break } else {return false; } Case '} ': if (!stk.empty () && stk.top () = = ' {') { Stk.pop (); ++i; Break } else {return false; }}} return Stk.empty (); }};
[Leetcode] 20-valid parentheses