Title: Effective brackets
Link to the original question: Effective brackets
This question is the beginning of my thinking is this:
each encounter '(', '{', '[' on the right to find out whether there is a corresponding brackets, but ignored the pair of brackets must be close to the limit.
After reading the explanations LeetCode Great God above, I feel really very clear ideas, and concise. Using a stack data structure , thinking is this:
for each character string traversal, each encounter '(', '{', '[', on the stack; encounter ')', '}', ' whether] 'on top of the stack is a view corresponding to the brackets, not the words of description is wrong, wrong judgment; if it is, then it popped. Finally, see if the stack is empty, empty, then tell the whole match is complete, true; otherwise false.
Link original answer: step by step analysis, graphical stack
code show as below:
class Solution {
public:
bool isValid(string s) {
unordered_map<char, int> m{{'(', 1},{'{', 2}, {'[', 3},{')', 4},{'}', 5},{']', 6}};
stack<char> st;
for(char c:s){
if(m[c] >= 1 && m[c] <= 3) st.push(c);
else if(!st.empty() && m[st.top()] == m[c] - 3) st.pop();
else return false;
}
if(!st.empty()) return false;
else return true;
}
};
Digression:
Not everyone can fame, some of us are doomed to look for the meaning of life in a bit of everyday life.
- Marc Levy "Big Bang"