In 16.12, book use a operator list to record multiply and addition. I used different method to let the multiply choice and multiplicand acting as arguments.
public class Solution
{
/**
*
* @param digits array of the digits.
* @param start starting point of current number.
* @param end ending point of current number.
* @param target target value.
* @param multiple true: the operator before the number is multiply, otherwise addition.
* @param multiplicand multiplicand of the multiply, only meaningful when {@link mulitple} is true.
* @return
*/
private String helper(final int[] digits, final int start, final int end, final int target, final boolean multiple, final int multiplicand)
{
// value of current number.
int num = 0;
int i;
// Because all number is positive and only addition and multiply. a number can't be larger than the target value.
for (i = start; i < end && num <= target; ++i)
{
num = num * 10 + digits[i];
// previous operator is addition.
if (!multiple)
{
// next operator is addition.
final String post = helper(digits, i + 1, end, target - num, false, 0);
if (null == post)
{
// Multiple.
final String part = helper(digits, i + 1, end, target, true, num);
if (null != part)
{
return String.valueOf(num) + "*" + part;
}
}
else
{
return String.valueOf(num) + "+" + post;
}
}
// previous operator is multiply
else
{
// Make sure the product is less than or equal to target value.
if (num * multiplicand <= target)
{
// Addition.
final String post = helper(digits, i + 1, end, target - num * multiplicand, false, 0);
if (null == post)
{
// Multiple.
final String part = helper(digits, i + 1, end, target, true, num * multiplicand);
if (null != part)
{
return String.valueOf(num) + "*" + part;
}
}
else
{
return String.valueOf(num) + "+" + post;
}
}
}
}
// Need to check when the remain part is a whole number.
if (!multiple)
{
return (start != end && i == end && num == target) ? String.valueOf(target) : null;
}
else
{
// Product is equal to target value.
return (start != end && i == end && num * multiplicand == target) ? String.valueOf(num) : null;
}
}
public String ExpressionSynthesis(final int[] digits, final int k)
{
return helper(digits, 0, digits.length, k, false, 0);
}
}