[LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
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 1 Input: "2-1-1" . ((2-1)-1) = 0 (2-(1-1)) = 2 Output: [0, 2] Example 2 Input: "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) = 10 Output: [-34, -14, -10, -10, 10] 给定一个含有数字和运算符的表达式,运算符可以是加减乘,在任意位置添加括号,求出所有可能的表达式值。 解法:递归 Java: public class Solution { public List<Integer> diffWaysToCompute(String input) { List<Integer> result = new ArrayList<>(); if