作者:hhha老窝_349 | 来源:互联网 | 2023-10-11 16:22
Given a string containing just the characters ‘(‘
, ‘)‘
, ‘{‘
, ‘}‘
, ‘[‘
and ‘]‘
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
var isValid = function(s) {
var a = [];
var len = s.length;
var temp;
if(len == 1) {
return false;
}
for(var i=0;i) {
if(s[i] == ‘(‘ || s[i] == ‘{‘ || s[i] == ‘[‘) {
a.unshift(s[i]);
}
if(s[i] == ‘)‘ ) {
temp = a.shift();
if(temp != ‘(‘)
return false;
}
if(s[i] == ‘}‘ ) {
temp = a.shift();
if(temp != ‘{‘)
return false;
}
if(s[i] == ‘]‘ ) {
temp = a.shift();
if(temp != ‘[‘)
return false;
}
}
if(a.length)
return false;
else
return true;
};
leetcode练习:20. Valid Parentheses