List<Integer> result = newArrayList<>(); if (getNextOpIndex(expression, 0) == -1) { result.add(Integer.parseInt(expression)); map.put(expression, result); return result; }
intlen= expression.length(); for (inti=0; i < len; ) { intindex= getNextOpIndex(expression, i); if (index == -1) { break; }
List<Integer> left = diffWaysToCompute(expression.substring(0, index), map); List<Integer> right = diffWaysToCompute(expression.substring(index + 1), map); result.addAll(calc(left, right, expression.charAt(index)));
i = index + 1; }
map.put(expression, result); return result; }
privateintgetNextOpIndex(String expression, int start) { for (inti= start; i < expression.length(); i++) { if (!Character.isDigit(expression.charAt(i))) { return i; } }
return -1; }
private List<Integer> calc(List<Integer> left, List<Integer> right, char op) { List<Integer> result = newArrayList<>(); for (Integer leftValue : left) { for (Integer rightValue : right) { int value; if (op == '+') { value = leftValue + rightValue; } elseif (op == '-') { value = leftValue - rightValue; } else { value = leftValue * rightValue; }