我试图理解递归并用
解决问题运营商= ['',' *'," +"]
输入:" 2224"
目标:24
输出= {" 22 + 2"," 2 + 22"," 24"}
这是我提出的代码。但它会产生无效的输出。
static List<String> output = new ArrayList<>();
static String[] generate_all_expressions(String s, long target) {
getExpressionsRecur(s, target, 0, null, 0);
String[] out = new String[output.size()];
return output.toArray(out);
}
static void getExpressionsRecur(String s, long target, int currentValue, String currExpression, int currIndex) {
if (currIndex == s.length()){
if (currentValue == target) {
output.add(currExpression);
}
return;
}
if (currentValue == target) {
output.add(currExpression);
return;
}
int currentPart = Integer.valueOf(s.substring(currIndex, currIndex+1));
if (currIndex == 0) {
getExpressionsRecur(s, target, currentPart, String.valueOf(currentPart), currIndex+1);
} else {
int value = Integer.valueOf(String.valueOf(currentValue) + String.valueOf(currentPart));
getExpressionsRecur(s, target, value , currExpression + "" + currentPart, currIndex+1);
getExpressionsRecur(s, target, (currentValue * currentPart), currExpression + "*" + currentPart, currIndex+1);
getExpressionsRecur(s, target, (currentValue + currentPart), currExpression + "+" + currentPart, currIndex+1);
}
}
它产生: {22 + 2,2 * 2 + 2 * 4,2 + 2 + 2 * 4}
有人可以帮助我发现错误吗?
答案 0 :(得分:0)
首先重新考虑你的递归,因为你没有遇到每一个案例。
如果需要,写出案例,可能以3位数字开头,因为案件较少;例如,在您的代码中,&#34; 24&#34;永远不会对它自己进行评估。
您正在为连接案例错误地计算值。 22 * 2将变为22 * 24.但是你说这个新表达式的值为444(应该是528)。你不能真正使用当前价值。可能你可以重构你的递归,使其更容易。
您可以根据需要使用我将这个表达式对象放在一起替换表达式String ... call expression.toString()和expression.value()。但是,即使在实现了这个或类似的表达式对象之后,您仍然需要对基本递归结构进行更改。
public class Expression{
Expression lExpression;
String lValue;
Expression rExpression;
String operator;
public Expression(String expression) {
int multIndex = expression.indexOf("*");
int addIndex = expression.indexOf("+");
if(multIndex == -1 && addIndex == -1) {
this.lExpression = new Expression(Integer.valueOf(expression));
return;
}
if(multIndex != -1 && multIndex < addIndex) {
this.lExpression = new Expression(expression.substring(0, addIndex));
this.operator = expression.substring(addIndex, addIndex+1);
this.rExpression = new Expression(expression.substring(addIndex+1));
}else {
if(addIndex == -1) {
addIndex = Integer.MAX_VALUE;
}
if(multIndex == -1) {
multIndex = Integer.MAX_VALUE;
}
int opIndex = multIndex < addIndex ? multIndex : addIndex;
this.lExpression = new Expression(expression.substring(0, opIndex));
this.operator = expression.substring(opIndex, opIndex+1);
this.rExpression = new Expression(expression.substring(opIndex+1)); }
}
public Expression(int value) {
this.lValue = String.valueOf(value);
this.lExpression = null;
this.rExpression = null;
this.operator = null;
}
@Override
public String toString() {
return (lExpression!=null ? lExpression : lValue) + (operator !=null ? operator: "") + (rExpression!=null ? rExpression : "");
}
public int value() {
if(lExpression == null) {
return Integer.valueOf(lValue);
}
if("*".equals(operator)) {
return lExpression.value() * rExpression.value() ;
}
if("+".equals(operator)) {
return lExpression.value() + rExpression.value();
}
return lExpression.value();
}
}