# 给表达式添加运算符

给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+- 或 * ,返回所有能够得到目标值的表达式。

 

示例 1:

输入: num = "123", target = 6
输出: ["1+2+3", "1*2*3"] 

示例 2:

输入: num = "232", target = 8
输出: ["2*3+2", "2+3*2"]

示例 3:

输入: num = "105", target = 5
输出: ["1*0+5","10-5"]

示例 4:

输入: num = "00", target = 0
输出: ["0+0", "0-0", "0*0"]

示例 5:

输入: num = "3456237490", target = 9191
输出: []

 

提示:

## template ```cpp #include using namespace std; class Solution { public: vector addOperators(string num, int target) { vector res; addOperatorsDFS(num, target, 0, 0, "", res); return res; } void addOperatorsDFS(string num, int target, long long diff, long long curNum, string out, vector &res) { if (num.size() == 0 && curNum == target) res.push_back(out); for (int i = 1; i <= num.size(); ++i) { string cur = num.substr(0, i); if (cur.size() > 1 && cur[0] == '0') return; string next = num.substr(i); if (out.size() > 0) { addOperatorsDFS(next, target, stoll(cur), curNum + stoll(cur), out + "+" + cur, res); addOperatorsDFS(next, target, -stoll(cur), curNum - stoll(cur), out + "-" + cur, res); addOperatorsDFS(next, target, diff * stoll(cur), (curNum - diff) + diff * stoll(cur), out + "*" + cur, res); } else addOperatorsDFS(next, target, stoll(cur), stoll(cur), cur, res); } } }; ``` ## 答案 ```cpp ``` ## 选项 ### A ```cpp ``` ### B ```cpp ``` ### C ```cpp ```