放个数组,遍历,左方框就加,右方框就减

三个数放个数组,最后三个数都是0,才是true,为负立即false

但是看relate是stack这个标签

不是很懂怎么和栈结合

bool isValid(string s) {
    stack<char> st;
    for(char c : s){        //遍历s里的char
        if(c == '('|| c == '{' || c == '['){
            st.push(c);
        }else{
            if(st.empty()) return false; 
            if(c == ')' && st.top() != '(') return false;
            if(c == '}' && st.top() != '{') return false;
            if(c == ']' && st.top() != '[') return false;
            st.pop();
        }
    }
    return st.empty();

看起来好像是我弄错description的意思了,我以为{[}]也是对的,不好好看

 


 

else if那一坨

要我肯定考虑== 而不是!=
bool isValid(string s) {
        stack<char> st;
        for(char c:s){
            if(c=='('||c=='{'||c=='[')
                st.push(c);
        else{
            if(st.empty())return false;
            if(c == ')' && st.top() == '(') {st.pop();continue;}
            if(c == '}' && st.top() == '{')  {st.pop();continue;}
            if(c == ']' && st.top() == '[')   {st.pop();continue;}
            else return false;   //但是少了这句就是错的,,,
} } return st.empty(); }

 



 

for那里是新知道的

所以搜一搜

the difference between for(char& c : s) and for(char c : s)?

新知识

由图可知因为copy成本太高,直接modify s,快一点,如果想保护s就const