71人阅读 评论(0)

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.

Examples:
“123”, 6 -> [“1+2+3”, “1*2*3”]
“232”, 8 -> [“2*3+2”, “2+3*2”]
“105”, 5 -> [“1*0+5”,”10-5”]
“00”, 0 -> [“0+0”, “0-0”, “0*0”]
“3456237490”, 9191 -> []

public class Solution {
private void dfs(List<String> ret, char[] path, int len, long left, long cur, char[] digits, int pos, int target) {
if (pos == digits.length) {
if (left + cur == target) ret.add(new String(path, 0, len));
return;
}
long n = 0;
int j = len + 1;
for (int i = pos; i < digits.length; i++) {
n = n * 10 + digits[i] - '0';
path[j++] = digits[i];
path[len] = '+';
dfs(ret, path, j, left + cur, n, digits, i + 1, target);
path[len] = '-';
dfs(ret, path, j, left + cur, -n, digits, i + 1, target);
path[len] = '*';
dfs(ret, path, j, left, cur * n, digits, i + 1, target);
if (digits[pos] == '0') break;
}
}
public List<String> addOperators(String num, int target) {
if (num.length() == 0) return ret;
char[] path = new char[num.length() * 2 - 1];
char[] digits = num.toCharArray();
long n = 0;
for (int i = 0; i < digits.length; i++) {
n = n * 10 + digits[i] - '0';
path[i] = digits[i];
dfs(ret, path, i + 1, 0, n, digits, i + 1, target);
if (n == 0) break;
}
return ret;
}
}

18ms,beats 98.68%,241ms,2.21%
cmershen的碎碎念：

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：64665次
• 积分：3289
• 等级：
• 排名：第10725名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：8条
文章分类
阅读排行
最新评论