注意边界值处理
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here if(!s.length()){ return false; } stack<char> parantheses; parantheses.push(s[0]); for(int i = 1; i<s.length();i++){ if(parantheses.empty()){ parantheses.push(s[i]); continue; } char top_char = parantheses.top(); if(top_char =='['&& s[i] ==']'){ parantheses.pop(); continue; }else if(top_char =='('&& s[i] ==')'){ parantheses.pop(); continue; } else if(top_char =='{'&& s[i] =='}'){ parantheses.pop(); continue; } parantheses.push(s[i]); } return parantheses.size()==0?true:false; } };