Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.
for (int a : p1) { for (int b : p2) { result.add(calc(a, b, c)); } } } // if input is a single number if (result.isEmpty()) { result.add(Integer.parseInt(input)); }
return result; }
privatebooleanisOperator(char c){ return c == '+' || c == '-' || c == '*'; }
privateintcalc(int a, int b, char c){ switch (c) { case'+': return a + b; case'-': return a - b; case'*': return a * b; //default should never be reached default: return Integer.MAX_VALUE; } } }