Given a string num that contains only digits and an integer target, return all possibilities to add the binary operators'+', '-', or'*'between the digits ofnumso that the resultant expression evaluates to thetargetvalue.
Example 1:
**Input:** num = "123", target = 6
**Output:** ["1*2*3","1+2+3"]
Example 2:
**Input:** num = "232", target = 8
**Output:** ["2*3+2","2+3*2"]
Example 3:
**Input:** num = "105", target = 5
**Output:** ["1*0+5","10-5"]
Example 4:
**Input:** num = "00", target = 0
**Output:** ["0*0","0+0","0-0"]
Example 5:
**Input:** num = "3456237490", target = 9191
**Output:** []
Constraints:
1 <= num.length <= 10
num consists of only digits.
-231 <= target <= 231 - 1
ac1: brute force DFS
classSolution {publicList<String> addOperators(String num,int target) {List<String> res =newArrayList<>();// edge casesif (num ==null||num.length() ==0) return res;dfs(num,0,0,0,true, target,"", res);return res; }privatevoiddfs(String num,int start,long val1,long val2,boolean plus,int target,String note,List<String> res) {long value = plus ? val1 + val2 : val1 - val2;// exitif (start >=num.length()) {if (value == target) res.add(note);return; }for (int i = start +1; i <=num.length(); i++) {if (num.charAt(start) =='0'&& i > start +1) break; // "05" is not allowed, 0 is finelong curr =Long.parseLong(num.substring(start, i));if (start ==0) { // start pointdfs(num, i, value, curr,true, target,""+ curr, res); } else {dfs(num, i, value, curr,true, target, note +"+"+ curr, res);dfs(num, i, value, curr,false, target, note +"-"+ curr, res);dfs(num, i, val1, val2 * curr, plus, target, note +"*"+ curr, res); } } }}/*backtracking find all result*/