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 *.
Example 112345Input: "2-1-1".((2-1)-1) = 0(2-(1-1)) = 2Output: [0, 2]
Example 212345678Input: "2*3-4*5"(2*(3-(4*5))) = -34((2*3)-(4*5)) = -14((2*(3-4))*5) = -10(2*((3-4)*5)) = -10(((2*3)-4)*5) = 10Output: [-34, -14, -10, -10, 10]
解法1:
用Divide & Conquer的思想,按照每一个运算符号分成左右两边。每边计算一下结果然后按照运算符号结合起来。当只有数字的时候所生成的res一定为空,这个时候就把数字加入res即可。
|
|