<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let isValid = function (s) {
let stack = [];
for (let i = 0; i < s.length; i++) {
const start = s[i];
if (start == '(' || start == '[' || start == '{') {
stack.push(s[i]);
} else {
const end = stack[stack.length - 1];
if (start == ")" && end == "(" || start == "]" && end == "[" || start == "}" && end == "{") {
console.log(stack.pop());
}
else {
return false;
}
}
}
return stack.length == 0;
};
console.log(isValid('(){}'));
console.log(isValid('(]{}'));
</script>
</body>
</html>
【LeetCode】20
最新推荐文章于 2024-07-12 16:55:56 发布