Article Directory
THE
problem
solution
Code
/*
思路: 涉及来回比较的问题选用栈来解决。将左符号存入栈,
然后遍历右符号,如果等于栈顶就继续,不然就
return false.
-
-
- -
- -
-
*/
class Solution {
public:
bool isValid(string s) {
int len = s.size();
stack<char> in;
for(int i =0;i<len;i++){
if(s[i]=='(')in.push(')');
else if(s[i]=='[')in.push(']');
else if(s[i]=='{')in.push('}');
if(s[i]==')'||s[i]=='}'||s[i]==']'){
if(!in.empty()&&s[i] == in.top()){
in.pop();
}
else return false;
}
}
return in.empty();
}
};
Summary and reflection
- Pay attention to consider the unparalleled judgment.