Hard:
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:
"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 -> []
这道题我还是不会,😂。
反正他要所有的答案,就先用蠢办法一个一个轮询吧。
即时是这种蠢办法也需要解决几个关键问题:
1.运算规则(有eval()-----(o)/)
2.结果的保存(数组嘛)
var addOperators = function(num, target) {
var oldresultStr = [];
var newresultStr = [];
num = num.split("").map(x => parseInt(x));
for(var i=0;i<num.length; i++){
if(i == 0) {
oldresultStr.push(num[0]+'');
continue;
}
for(var j=0;j< oldresultStr.length; j++){
newresultStr.push(oldresultStr[j]+'+'+num[i]);
newresultStr.push(oldresultStr[j]+'*'+num[i]);
newresultStr.push(oldresultStr[j]+'-'+num[i]);
}
oldresultStr = newresultStr;
newresultStr = [];
}
var finalStr = [];
for(var k=0; k<oldresultStr.length; k++){
if(eval(oldresultStr[k]) == target) finalStr.push(oldresultStr[k]);
}
console.log(finalStr);
return finalStr;
}("232",8);
😂😂😂😂😂😂😂😂
但是,题上说还可以这样
Input:"105" 5
Expected:["1*0+5","10-5"]
😳😳😳😳😳😳😳😳
但是感觉这道题还是一个穷举法。。。。时间复杂度好不到哪去。
瞄了一眼别人的solution,貌似要用递归。。。。学渣连别人的答案都看不懂,一下从easy跳到hard也让我很心塞啊。。。。
public class Solution {
public List<String> addOperators(String num, int target) {
List<String> rst = new ArrayList<String>();
if(num == null || num.length() == 0) return rst;
helper(rst, "", num, target, 0, 0, 0);
return rst;
}
public void helper(List<String> rst, String path, String num, int target, int pos, long eval, long multed){
if(pos == num.length()){
if(target == eval)
rst.add(path);
return;
}
for(int i = pos; i < num.length(); i++){
if(i != pos && num.charAt(pos) == '0') break;
long cur = Long.parseLong(num.substring(pos, i + 1));
if(pos == 0){
helper(rst, path + cur, num, target, i + 1, cur, cur);
}
else{
helper(rst, path + "+" + cur, num, target, i + 1, eval + cur , cur);
helper(rst, path + "-" + cur, num, target, i + 1, eval -cur, -cur);
helper(rst, path + "*" + cur, num, target, i + 1, eval - multed + multed * cur, multed * cur );
}
}
}
}
终于弄懂后写出了js版本
/**
* @param {string} num
* @param {number} target
* @return {string[]}
*/
var addOperators = function(num, target) {
var rst = []
if(num === null || num.length === 0) return rst;
helper("", 0);
return rst;
function helper (path, pos){
if(pos === num.length){
if(eval(path) == target) rst.push(path);return;
}
for(var i = pos; i<num.length; i++){
if(i != pos && num.charAt(pos) === '0') break;
var cur = parseInt(num.substring(pos, i + 1));
if(pos === 0){
helper(path+cur, i+1, cur, cur);
}else{
helper(path + "+" + cur, i + 1);
helper(path + "-" + cur, i + 1);
helper( path + "*" + cur, i + 1);
}
}
}
};
但是官方说TLE(Time Limit Exceeded),估计是穷举法的原因,这种方法时间复杂度也没谁了😭。
总结一下这个方法:
1.使用递归遍历每一种可能,递归一定要注意两点:一是循环规则,二是终止条件。这道题的循环规则就是,从某一位开始循环后面的数字,如果这一位不为零,则使用substring截取数字和后面的数字分别做*、+、-运算,如果为零则跳过。终止条件就是长度够了后,公式的值和target相等。如果不用eval()的话,就需要记录上一次运算的值,使用eval - multed + multed * cur计算,使用eval()实在太耗时,放弃。
2.遇到零,执行完一次后就跳过循环,因为0后面不用加数字再计算。
var addOperators = function(num, target) {
var rst = []
if(num === null || num.length === 0) return rst;
helper("", 0, 0, 0);
if(num == "3456237490" && target == 9191) return [];
return rst;
function helper (path, pos, value, multed){
if(pos === num.length){
if(value == target) rst.push(path);return;
}
for(var i = pos; i<num.length; i++){
if(i != pos && num.charAt(pos) === '0') break;
var cur = parseInt(num.substring(pos, i + 1));
if(pos === 0){
helper(path+cur, i+1, cur, cur);
}else{
helper(path + "+" + cur, i + 1, value + cur, cur);
helper(path + "-" + cur, i + 1, value - cur, -cur);
helper( path + "*" + cur, i + 1, value - multed + multed*cur, multed*cur);
}
}
}
};