description:
check whether the (){}[] is valid(is pair)
Note:Example:
Example 1:Input: "()"Output: trueExample 2:Input: "()[]{}"Output: trueExample 3:Input: "(]"Output: falseExample 4:Input: "([)]"Output: falseExample 5:Input: "{[]}"Output: true
my answer:
my answer
一眼想到堆栈,奈何堆栈没有想起我,堆栈的用法get一下ok吧...
大佬的answer:
class Solution {public: bool isValid(string s) { stackres; for(int i = 0; i < s.size(); ++i){ if(s[i] == '(' || s[i] == '[' || s[i] == '{') res.push(s[i]); else{ if(res.empty()) return false; if(s[i] == ')' && res.top() != '(')return false; if(s[i] == ']' && res.top() != '[')return false; if(s[i] == '}' && res.top() != '{')return false; res.pop(); } } return res.empty(); }};
relative point get√:
stackres; //definite a stack
stack.top(); // the element on the top of the stack stack.push(xxx); // push a element on the top of the stack stack.pop(); // pop the element on the top of the stack stack.empty();