对称符号问题,用数组和top指针模拟栈为什么会出错,后来用stack<char>来做能ac了

class Solution {
public:
    bool isValid(string s) {
     string st;
     int top=-1;
     int i=0;
     int n=s.length();
     while(i<n&&top>=-1){
         if(top==-1){
             if(s[i]==')'||s[i]==']'||s[i]=='}')
             return false;
             }
         if(s[i]=='('||s[i]=='['||s[i]=='{'){
             st[++top]=s[i];
        
             
         }
        else if(s[i]==')'){
             if(st[top]=='(')
                top--;
             else
                 return false ;
         }
         else if(s[i]==']'){
             if(st[top]=='[')
                top--;
             else
                 return false ;
         }
        else  if(s[i]=='}'){
             if(st[top]=='{')
                top--;
             else
                 return false ;
         }
         
         i++;
     }
     if(top>=0)
        return false;
    
     return true;
    }
};

 

可能出现问题的原因有很多,不过使用数组和 top 指针模拟栈时