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"
Output: [0, 2]
Explanation: 
((2-1)-1) = 0 
(2-(1-1)) = 2
Example 2:
Input: "2*3-4*5"
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








2*3-4*5:
2                 *3-4*5"
2*3            -4*5"
2*3-4        *5"





*3-4*5 : 
 *3            -4*5"
 *3-4        *5"



2*3-4 : 
2        *3-4
2*3        -4




https://www.youtube.com/watch?v=gxYV8eZY0eQ

Simililar to word break 

dfs 
Dfs + memo
Dp 




Dfs :

// if the input.length() = n , then the time complexity is n * n !  

// others code , kinda understand 
class Solution {
    public List<Integer> diffWaysToCompute(String input) {
        // not the dfs sample i use 
        List<Integer> res = new ArrayList<>();
        for(int i = 0; i < input.length(); i++){         // time : input.length()
            char cur = input.charAt(i);
            if(!Character.isDigit(cur)){ 
                String part1 = input.substring(0, i);            // time:  part of the the input.length()
                String part2 = input.substring(i + 1);    // time :  part of the input.length() 
                List<Integer> res1 = diffWaysToCompute(part1);
                List<Integer> res2 = diffWaysToCompute(part2);
                for(int p1 : res1){
                    for(int p2 : res2){
                        int p = 0;
                        switch(cur){
                            case '+': p = p1 + p2;
                                break;
                            case '-': p = p1 - p2;
                                break;
                            case '*': p = p1 * p2;
                                break;
                        }
                        res.add(p);
                    }
                }
            }
        }
        if(res.size() == 0){
            res.add(Integer.valueOf(input));
        }
        return res;
        
    }
}




// Character.isDigit(cur)
 //  switch(cur){ case ' break;
// (Integer.valueOf



// dfs + memo  
// time :  n ! 
// space: 
// need to walk thru an example, and to understand how its top down and 
// still don’t understand why the map is updated at the bottom 

class Solution {
    HashMap<String, List<Integer>> map = new HashMap<>();
    public List<Integer> diffWaysToCompute(String input) {
        if(map.containsKey(input)) return map.get(input);
        // use a map to cache result 
        
        // not the dfs sample i use 
        List<Integer> res = new ArrayList<>();
        for(int i = 0; i < input.length(); i++){         // input.length() 
            char cur = input.charAt(i);
            if(!Character.isDigit(cur)){
                String part1 = input.substring(0, i);
                String part2 = input.substring(i + 1);
                List<Integer> res1 = diffWaysToCompute(part1);
                List<Integer> res2 = diffWaysToCompute(part2);
                for(int p1 : res1){
                    for(int p2 : res2){
                        int p = 0;
                        switch(cur){
                            case '+': p = p1 + p2;
                                break;
                            case '-': p = p1 - p2;
                                break;
                            case '*': p = p1 * p2;
                                break;
                        }
                        res.add(p);
                    }
                }
            }
        }
        if(res.size() == 0){
            res.add(Integer.valueOf(input));
        }
        map.put(input, res);
        return res;
        
    }
}

 

posted on 2018-11-06 09:30  猪猪&#128055;  阅读(106)  评论(0)    收藏  举报

导航