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指针模拟栈为什么会出错,后来用stack<char>来做能ac了
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-