-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionAddOperators.V1.js
More file actions
46 lines (40 loc) · 1.02 KB
/
Copy pathExpressionAddOperators.V1.js
File metadata and controls
46 lines (40 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// https://leetcode-cn.com/problems/expression-add-operators/
var Test = require('./Common/Test');
var addOperators = function (num, target) {
const operators = ['+', '-', '*', ''];
const result = [];
backTracing([], 0);
return result;
function backTracing(array, index) {
array.push(num[index++]);
if (index == num.length) {
const expr = array.join('');
if (eval(expr) == target) {
result.push(expr);
}
}
else {
for (const operator of operators) {
if (!(array[array.length - 1] == '0' && operator == '')) {
array.push(operator);
backTracing(array, index);
array.pop();
}
}
}
array.pop();
}
};
function test(num, target) {
Test.test(addOperators, num, target);
}
test("123", 6);
test("123", 15);
test("232", 8)
test("105", 5)
test("00", 0)
test("3456237490", 9191)
// "123"
// 6
// "123"
// 15