本文共 884 字,大约阅读时间需要 2 分钟。
?????????????????????????????????????????????????
???????
true?false???????????????
stack<char> ????????????????????
'(', '{', '[')??????????????')', '}', ']')?????????????? false???????
false?true??????????????????????????????????? O(n)??? n ?????????????? O(n)?
#include#include using namespace std;bool EffectiveBrackets(string s) { if (s.empty()) return true; if (s.size() % 2 != 0) return false; stack st; for (char c : s) { if (c == '(' || c == '{' || c == '[') { st.push(c == '(' ? ')' : (c == '{' ? '}' : ']')); } else { if (st.empty() || st.top() != c) { return false; } st.pop(); } } return st.empty();}
转载地址:http://kiuy.baihongyu.com/