算法与数据结构学习
This commit is contained in:
20
Chapter4/test1.mjs
Normal file
20
Chapter4/test1.mjs
Normal 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')
|
Reference in New Issue
Block a user