-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddOperators.cpp
More file actions
27 lines (26 loc) · 893 Bytes
/
addOperators.cpp
File metadata and controls
27 lines (26 loc) · 893 Bytes
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
class Solution {
public:
vector<string> addOperators(string num, int target) {
vector<string> res;
dfs(num, target, 0, 0, 0, "", res);
return res;
}
void dfs(string& s, int target, int pos, long cv, long pv, string r, vector<string>& res) {
if (pos == s.size() && cv == target) {
res.push_back(r);
return;
}
for (int i = 1; i <= s.size() - pos; i++) {
string t = s.substr(pos, i);
if (i > 1 && t[0] == '0') continue; // preceding
long n = stol(t);
if (pos == 0) {
dfs(s, target, i, n, n, t, res);
continue;
}
dfs(s, target, pos+i, cv+n, n, r+"+"+t, res);
dfs(s, target, pos+i, cv-n, -n, r+"-"+t, res);
dfs(s, target, pos+i, cv-pv+pv*n, pv*n, r+"*"+t, res);
}
}
};