Question
Formatted question description: https://leetcode.ca/all/282.html
282 Expression Add Operators
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.
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: []
Algorithm
We need two variables diff and curNum, one is used to record the value to be changed, the other is the value after the current operation, and they all need to use long type, because the string is converted to int type, it is easy to overflow.
For addition and subtraction, diff is the negative value of the number to be added and the number to be subtracted.
For multiplication, it’s a bit more complicated, diff is the diff of the last change multiplied by the number to be multiplied.
For example, for example, 2+32, when the calculation is about to multiply by 2, the last cycle of curNum = 5, diff = 3, and if we want to calculate this multiplication by 2, the new change value diff should It is 32=6, and we need to remove the result of the previous +3 operation, and add a new diff, namely (5-3)+6=8, which is the value of the new expression 2+3*2.
Another thing to note is that if the input is “000”, 0, the following errors are prone to occur:
Wrong: ["0+0+0","0+0-0","0+0*0","0-0+0","0-0-0","0-0*0" ,"0*0+0","0*0-0","0*0*0","0+00","0-00","0*00","00+0"," 00-0","00*0","000"]
Correct:["0*0*0","0*0+0","0*0-0","0+0*0","0+0+0","0+0-0" ,"0-0*0","0-0+0","0-0-0"]
Code
Java
import java.util.ArrayList;
import java.util.List;
public class Expression_Add_Operators {
class Solution {
public List<String> addOperators(String num, int target) {
ArrayList<String> result = new ArrayList<String>();
if (num.length() == 0) {
return result;
}
dfs(num, target, 0, 0, "", result);
return result;
}
// `diff` is for rollback previous `*` operation
void dfs(String digitString, int target, long diff, long curNum, String oneResult, ArrayList<String> res) {
if (digitString.length() == 0 && curNum == target) {
res.add(oneResult);
return;
}
for (int i = 1; i <= digitString.length(); ++i) {
// divide and conquer
String cur = digitString.substring(0, i);
String next = digitString.substring(i);
if (cur.length() > 1 && cur.charAt(0) == '0') { // @note: skip '000', but single '0' is ok
return;
}
if (oneResult.length() > 0) {
dfs(next, target, Long.valueOf(cur), curNum + Long.valueOf(cur), oneResult + "+" + cur, res);
dfs(next, target, -1 * Long.valueOf(cur), curNum - Long.valueOf(cur), oneResult + "-" + cur, res);
dfs(next, target, diff * Long.valueOf(cur), (curNum - diff) + diff * Long.valueOf(cur), oneResult + "*" + cur, res);
} else {
dfs(next, target, Long.valueOf(cur), Long.valueOf(cur), cur, res); // use `cur` as oneResult
}
}
}
}
}