算法与数据结构学习

This commit is contained in:
mol
2024-07-04 22:38:39 +08:00
parent fde7676684
commit 5b61f1d7a1
7 changed files with 610 additions and 0 deletions

20
Chapter4/test1.mjs Normal file
View File

@ -0,0 +1,20 @@
import Stack from "./stack.mjs";
// 找出缺失的括号
function findMissingParentheses(expression) {
const stack = new Stack();
const expressionList = expression.split('');
for (let i = 0; i < expressionList.length; i++) {
const char = expressionList[i];
if (char === '(') {
stack.push(i);
}
if (char === ')') {
stack.pop();
}
}
if (stack.length() > 0) {
console.log('missing parent index:', stack.pop());
}
}
findMissingParentheses('2.3 + 23 / 12 + (3.14159 x 0.24')