forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionAddOperators.cpp
81 lines (71 loc) · 3.28 KB
/
ExpressionAddOperators.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Source : https://leetcode.com/problems/expression-add-operators/
// Author : Hao Chen
// Date : 2016-01-16
/***************************************************************************************
*
* 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 -> []
*
* Credits:Special thanks to @davidtan1890 for adding this problem and creating all
* test cases.
***************************************************************************************/
class Solution {
public:
vector<string> addOperators(string num, int target) {
vector<string> result;
if (num.size() == 0) return result;
helper(num, target, result, "", 0, 0, 0, ' ');
return result;
}
//DFS algorithm
void helper(const string &num, const int target, //`num` and `target` never change
vector<string>& result, // the array store all of the answers
string solution, //the current potential answer.
int idx, // the current index of `num` array
long long val, // the current value we calculated so far
long long prev, // the lastest value we used for calculation, which used for operation prioirty adjustment
char preop ) // the latest "+" or "-" operation, which used for operation prioirty adjustment
{
if (target == val && idx == num.size()){
result.push_back(solution);
return;
}
if (idx == num.size()) return;
string n;
long long v=0;
for(int i=idx; i<num.size(); i++) {
//get rid of the number which start by "0"
//e.g. "05" is not the case.
if (n=="0") return;
n = n + num[i];
v = v*10 + num[i]-'0';
if (solution.size()==0){
// the first time for initialization
helper(num, target, result, n, i+1, v, 0, ' ');
}else{
// '+' or '-' needn't to adjust the priority
helper(num, target, result, solution + '+' + n, i+1, val+v, v, '+');
helper(num, target, result, solution + '-' + n, i+1, val-v, v, '-');
//if we meet multiply operation, we need adjust the calcualtion priority
// e.g. if the previous value is calculated by 2+3=5,
// then if we need to multipy 4, it is not 5*4, it is 2+3*4=2+12=24
// we need be careful about multiply again, such as: 2+3*4*5
if (preop=='+') {
helper(num, target, result, solution + '*' + n, i+1, (val-prev)+prev*v, prev*v, preop);
}else if (preop=='-'){
helper(num, target, result, solution + '*' + n, i+1, (val+prev)-prev*v, prev*v, preop);
}else {
helper(num, target, result, solution + '*' + n, i+1, val*v, v, '*');
}
}
}
}
};