给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +
, -
以及 *
。
示例 1:
输入:"2-1-1"
输出:[0, 2]
解释: ((2-1)-1) = 0 (2-(1-1)) = 2
示例 2:
输入:"2*3-4*5"
输出:[-34, -14, -10, -10, 10]
解释: (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10
题目标签:Divide and Conquer
题目链接:LeetCode / LeetCode中国
分治法
Language | Runtime | Memory |
---|---|---|
cpp | 8 ms | 15.6 MB |
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
static unordered_map<string, vector<int>> cache;
bool isNum = true;
vector<int> res;
for (int i = 0; i < input.length(); i++) {
if (!isdigit(input[i])) {
isNum = false;
vector<int> a = diffWaysToCompute(input.substr(0, i));
vector<int> b = diffWaysToCompute(input.substr(i + 1));
for (int ai : a) {
for (int bi : b) {
switch(input[i]) {
case '+': res.push_back(ai + bi); break;
case '-': res.push_back(ai - bi); break;
case '*': res.push_back(ai * bi); break;
case '/': res.push_back(ai / bi); break;
}
}
}
}
}
if (isNum) {
res.push_back(stoi(input));
} else {
}
return cache[input] = res;
}
};