-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathExpression_Add_Operators.cpp
29 lines (28 loc) · 1.24 KB
/
Expression_Add_Operators.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
class Solution {
public:
void addOperatorsUtil(string num, int target, long long diff, long long currValue, string solution, vector<string>& result) {
if(num.length() == 0 and target == currValue) {
result.push_back(solution);
}
for(int i = 1; i <= num.length(); ++i) {
string curr = num.substr(0, i);
if(curr.length() > 1 and curr[0] == '0') {
break;
}
string next = num.substr(i);
if(solution.length() > 0) {
addOperatorsUtil(next, target, stoll(curr), currValue + stoll(curr), solution + '+' + curr, result);
addOperatorsUtil(next, target, -stoll(curr), currValue - stoll(curr), solution + '-' + curr, result);
addOperatorsUtil(next, target, diff * stoll(curr), (currValue - diff) + diff * stoll(curr), solution + '*' + curr, result);
} else {
addOperatorsUtil(next, target, stoll(curr), currValue + stoll(curr), curr, result);
}
}
}
vector<string> addOperators(string num, int target) {
vector<string> result;
string solution;
addOperatorsUtil(num, target, 0LL, 0LL, solution, result);
return result;
}
};