20 lines
520 B
JavaScript
20 lines
520 B
JavaScript
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') |