forked from jyxia/LeetCode-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241-differentWaysAddParentheses.js
32 lines (30 loc) · 1.17 KB
/
241-differentWaysAddParentheses.js
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
/**
* @param {string} input
* @return {number[]}
*/
// slow, we can use dp to cache some results
var diffWaysToCompute = function(input) {
var results = [];
for (var i = 0; i < input.length; i++) {
if (input[i] === '-' || input[i] === '+' || input[i] === '*') {
console.log(input);
var inputLeft = diffWaysToCompute(input.substring(0, i));
var inputRight = diffWaysToCompute(input.substring(i + 1));
for (var j = 0; j < inputLeft.length; j++) {
for (var k = 0; k < inputRight.length; k++) {
var result ;
if (input[i] === '-') {
result = parseInt(inputLeft[j]) - parseInt(inputRight[k]);
} else if (input[i] === '+') {
result = parseInt(inputLeft[j]) + parseInt(inputRight[k]);
} else if (input[i] === '*') {
result = parseInt(inputLeft[j]) * parseInt(inputRight[k]);
}
results.push(result);
}
}
}
}
if (results.length === 0) results.push(parseInt(input));
return results;
};