zoukankan      html  css  js  c++  java
  • [LeetCode#241]Different Ways to Add Parentheses

    Problem:

    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]

    Analysis:

    At first glance, there problem is really complex!!! But once you get the point, you feel so surprised that this problem is so elegant and simple.
    
    Basic idea: divide and conquer.
    Assumption 1:  input = a - b + c + e * f
    How could we add parenthess onto the input string?
    
    Add all parenthesses at one time? 
    That's too complex...
    How about add parenthess recursively???
    For each input, we only add two parenthess, then pass the task of adding parenthess into next level.
    Step 1: input = (a - b) + (c + e * f)
    Step 2: add_compute(a - b), add_compute(c + e * f)
    
    Key 1: the valid way to add bracket, choice a operator, then cut into two parts.
    if (c == '+' || c == '-' || c == '*') {
        List<Integer> left = diffWaysToCompute(input.substring(0, i));
        List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
        ...
    }
    
    Key 2: Use the number string(not include operand) as base case! Since we always divide the input string against a operator, we must be able to reach the base case: "input is a string representation of number". 
    if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
        ret.add(Integer.valueOf(input));
        return ret;
    }
    Note: iff use input.indexOf('+') must in the form of input.indexOf('+') != -1.
    
    
    Key 3: use operand over the return value from child branch.
    for (int m = 0; m < left.size(); m++) {
        for (int n = 0; n < right.size(); n++) {
            int num1 = left.get(m);
            int num2 = right.get(n);
            if (c == '+') 
                ret.add(num1 + num2);
            else if (c == '-')
                ret.add(num1 - num2);
            else
                ret.add(num1 * num2);
        }
    }

    Wrong solution:

    public class Solution {
        public List<Integer> diffWaysToCompute(String input) {
            List<Integer> ret = new ArrayList<Integer> ();
            int len = input.length();
            if (len == 1) {
                ret.add(Integer.valueOf(input.charAt(0) + ""));
                return ret;
            }
            for (int i = 0; i < len; i++) {
                char c = input.charAt(i);
                if (c == '+' || c == '-' || c == '*') {
                    List<Integer> left = diffWaysToCompute(input.substring(0, i));
                    List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
                    for (int m = 0; m < left.size(); m++) {
                        for (int n = 0; n < right.size(); n++) {
                            if (c == '+') 
                                ret.add(m + n);
                            else if (c == '-')
                                ret.add(m - n);
                            else
                                ret.add(m * n);
                        }
                    }
                }
            }
            return ret;
        }
    }

    Mistake analysis:

    Mistake 1:
    The stereotype of using CharAt(). In this problem, a number could be represented by using multi charcacters together.
    Fix:
    if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
        ret.add(Integer.valueOf(input));
        return ret;
    }
    
    Mistake 2:
    Forget to get the number from return list, but use index.
    Fix:
    int num1 = left.get(m);
    int num2 = right.get(n);
    if (c == '+') {
        ...
    }

    Solution:

    public class Solution {
        public List<Integer> diffWaysToCompute(String input) {
            List<Integer> ret = new ArrayList<Integer> ();
            int len = input.length();
            if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
                ret.add(Integer.valueOf(input));
                return ret;
            }
            for (int i = 0; i < len; i++) {
                char c = input.charAt(i);
                if (c == '+' || c == '-' || c == '*') {
                    List<Integer> left = diffWaysToCompute(input.substring(0, i));
                    List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
                    for (int m = 0; m < left.size(); m++) {
                        for (int n = 0; n < right.size(); n++) {
                            int num1 = left.get(m);
                            int num2 = right.get(n);
                            if (c == '+') 
                                ret.add(num1 + num2);
                            else if (c == '-')
                                ret.add(num1 - num2);
                            else
                                ret.add(num1 * num2);
                        }
                    }
                }
            }
            return ret;
        }
    }
  • 相关阅读:
    asp.net 获取当前项目的根目录路径
    asp.net 中 UEditor 图片和附件上传失败的处理方法
    [LOJ#2331]「清华集训 2017」某位歌姬的故事
    [LOJ#2330]「清华集训 2017」榕树之心
    [LOJ#2329]「清华集训 2017」我的生命已如风中残烛
    [LOJ#2328]「清华集训 2017」避难所
    [LOJ#2327]「清华集训 2017」福若格斯
    [LOJ#2326]「清华集训 2017」简单数据结构
    [LOJ#2325]「清华集训 2017」小Y和恐怖的奴隶主
    [LOJ#2324]「清华集训 2017」小Y和二叉树
  • 原文地址:https://www.cnblogs.com/airwindow/p/4781118.html
Copyright © 2011-2022 走看看