/**有效的括号
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
let map = new Map([
[")", "("],
["]", "["],
["}", "{"],
]);
if (s.length == 1) {
return false;
}
let stack = [s[0]];
let index = 0;
let i = 1;
while (i < s.length) {
if (map.has(s[i]) && map.get(s[i]) == stack[index]) {
stack.pop();
index--;
i++;
} else {
stack.push(s[i]);
index++;
i++;
}
}
if (!stack.length) {
return true;
} else {
return false;
}
};
let s = "([]){"
console.log(isValid(s));
20.手写JavaScript 有效的括号
最新推荐文章于 2024-05-21 19:09:41 发布