Leetcode 0241: Different Ways to Add Parentheses

Title description:

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 *.

Chinese description:

Given a string containing numbers and operators, add parentheses to the expression and change the priority of operations to find different results. You need to give the result of all possible combinations. Valid operators include +,-and *.

Example 1:

Input: “2-1-1”
Output: [0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2

Example 2:

Input: “23-45”
Output: [-34, -14, -10, -10, 10]
Explanation:
(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

Example 3:

Input: nums = [1,-1], k = 1
Output: [1,-1]

Example 4:

Input: nums = [9,11], k = 2
Output: [11]

Example 5:

Input: nums = [4,-2], k = 2
Output: [4]

Constraints:

1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length

Time complexity: O O O( C n C_{n} Cn)
Cattleya Number Divide and Conquer

  1. The entire string expression can be regarded as a middle-order traversal of different binary trees. The number of child nodes is the same as the number in parentheses to represent the same nearest root node. Different binary tree in-order traversals are the results displayed by different expressions of the string.
  2. Traverse the entire string, and when it encounters the operator'+','-', or'*', you can take the operator character as the root node, and use the left half [0,i-1] as the left subtree, Take the right side [i + 1,n-1] as the right subtree, and recurse the two subtrees to find out all the cases that meet the meaning of the question.
  3. Any binary tree in the set of left subtrees and any binary tree in the set of right subtrees can be spliced ​​with the current root node i to form a new binary tree, that is, the value of the left half of the string is formed by adding brackets in different ways The set of values ​​formed by adding parentheses to the set and the left half of the string in different ways can be combined again with the current characters'+','-','*' and parentheses to form a new set of sets
  4. When the recursive string is just a number, return the number directly
class Solution {
    
    

    public List<Integer> diffWaysToCompute(String input) {
    
    
        List<Integer> res = new ArrayList<>();
        for (int i=0; i<input.length(); i++) {
    
    
            char op = input.charAt(i);
            if (op == '-' ||
                op == '*' ||
                op == '+' ) {
    
    
                String part1 = input.substring(0, i);
                String part2 = input.substring(i+1);
                List<Integer> part1Res = diffWaysToCompute(part1);
                List<Integer> part2Res = diffWaysToCompute(part2);
                for (Integer p1: part1Res) {
    
    
                    for (Integer p2:part2Res) {
    
    
                        int tmp = 0;
                        if(op == '-'){
    
    
                            tmp = p1-p2;
                        }else if(op == '*'){
    
    
                            tmp = p1*p2;
                        }else if(op == '+' ){
    
    
                            tmp = p1+p2;
                        }
                        res.add(tmp);
                    }
                }
            }
        }
        if (res.size() == 0) {
    
    
            res.add(Integer.valueOf(input));
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43946031/article/details/114024505