您的位置:首页 > 运维架构

Expression Add Operators -- leetcode

2015-10-14 10:19 218 查看
Given a string that contains only digits 
0-9
 and a target value, return all possibilities
to add binary operators (not unary) 
+
-
,
or 
*
 between the digits so they evaluate to the target value.

Examples: 

"123", 6 -> ["1+2+3", "1*2*3"]
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []


算法一,

每得到一个新的整数,对其进行+, -, *三种运算。

使用dfs递归,免去重复计算。

此处使用sum, mul两个变量记录中间结果。 sum 存储累加和, mul 存储乘积结果。

之所以用两个,是基于如下情况:

1. 当我们在一个整数前添加+号时, 此整数后面紧跟的是*运算的话,我们则不能将此整数运算到sum中

2. -号同上

3. 当添加*时,我们只需要将该整数累乘进mul中,而sum没有变化。

4. 何时将mul值运算进sum中呢,就是在步骤1,2时,将旧的mul提交进sum中,将新的整数保存到mul。 

另一个注意事项是,在处理整数溢出,此处使用long,而不是int。 因为long在64位系统下是64bit。

在leetcode上实际执行时间为244ms。

class Solution {
public:
vector<string> addOperators(string num, int target) {
if (num.empty())
return {};
vector<string> ans;
addOperators(ans, num, 0, target, 0, 0, "");
return ans;
}

void addOperators(vector<string>& ans, string input, int index, int target, long sum, long mul, string exp) {
if (input.size() == index) {
if (sum+mul == target)
ans.push_back(exp.substr(1));
return;
}

exp.push_back('#');
const int opi = exp.size()-1;
long num = 0;
for (int i=index; i<input.size(); i++) {
if (index+1 == i && input[index] == '0')
break; // exclude numbers start with 0, e.g. 0xx
num = num * 10 + input[i] - '0';
exp.push_back(input[i]);
exp[opi] = '+';
addOperators(ans, input, i+1, target, sum+mul, num, exp);
if (!index)
continue;
exp[opi] = '-';
addOperators(ans, input, i+1, target, sum+mul, -num, exp);
exp[opi] = '*';
addOperators(ans, input, i+1, target, sum, mul*num, exp);
}
}
};


算法二,

此算法比较直观。

当在两个数字之间,我们有4种情况:

1, 添加+,-, *共三种情况。

2. 什么也不添加。即构成一个更大的整数。

根据上面的规则,穷举所有的情况,形成一个个表达式。然后执行该表达式。

为了节约时间,可以边构造表达式,边计算。 因为表达式之间的前面相同部分很大,不必重复进行计算。

此代码比上面略长,原因在于此处使用两段时提交。先将数从num提交到mul中,然后再从mul提交到sum中。

故在表达计算结束时,还需要提交2次,才能得到最终结果。

在leetcode上实际执行时间为544ms。

class Solution {
public:
vector<string> addOperators(string num, int target) {
if (num.empty())
return {};
vector<string> ans;
addOperators(ans, num, 0, target, 0, 0, 0, '+', num[0], "");
return ans;
}

void addOperators(vector<string>& ans, string input, int index, int target, long sum, long mul, long num, char op, char ch, string exp) {
if (isdigit(ch) && (exp.size()>=2 && exp.back() == '0' && !isdigit(exp[exp.size()-2]) ||
exp.size()==1 && exp.back() == '0'))
return; // exclude integer like 0xx, +0xx, -0xx, etc.
exp.push_back(ch);
if (isdigit(ch)) {
num = num * 10 + ch - '0';
++index;
if (index == input.size()) {
if (op == '+' || op == '-') {
sum += mul;
mul = (44 - op) * num; // + is 43, - is 45, in ascii
}
else
mul *= num;
sum += mul;
if (sum == target)
ans.push_back(exp);
}
else {
addOperators(ans, input, index, target, sum, mul, num, op, '+', exp);
addOperators(ans, input, index, target, sum, mul, num, op, '-', exp);
addOperators(ans, input, index, target, sum, mul, num, op, '*', exp);
addOperators(ans, input, index, target, sum, mul, num, op, input[index], exp);
}
}
else {
if (op == '+' || op == '-') {
sum += mul;
mul = (44 - op) * num; // + is 43, - is 45, in ascii
}
else
mul *= num;

addOperators(ans, input, index, target, sum, mul, 0, ch, input[index], exp);
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 递归