LeetCode 241. 为运算表达式设计优先级

241. 为运算表达式设计优先级

LeetCode 241. 为运算表达式设计优先级_第1张图片 

LeetCode 241. 为运算表达式设计优先级_第2张图片

【分治】按照运算符号划分左右,递归处理左右后得到左右两侧可能的结果进行组合。

class Solution {

    // 分治 9:30 10:34

    List list = new ArrayList();

    List diff(int start, int end) {
        if (start == end) {
            String str = list.get(start);
            return new ArrayList() {{ add(Integer.parseInt(str)); }};
        } 
        List ans = new ArrayList();
        for (var i = start; i <= end; i++) {
            String str = list.get(i);
            if (str.equals("+") || str.equals("-") || str.equals("*")) {
                List left = diff(start, i - 1);
                List right = diff(i + 1, end);
                for (var x: left) {
                    for (var y: right) {
                        if (str.equals("+")) {
                            ans.add(x + y);
                        } else if(str.equals("-")) {
                            ans.add(x - y);
                        } else if (str.equals("*")) {
                            ans.add(x * y);
                        }
                    }
                }
            }
        }
        return ans;
    }

    public List diffWaysToCompute(String expression) {
        String num = "";
        for (var i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if (c >= '0' && c <= '9') {
                num += c;
            } else {
                list.add(num);
                num = "";
                list.add("" + c);
            }
        }
        list.add(num);
        return diff(0, list.size() - 1);
    }
}

【优化】优化字符串处理,用StringBuilder进行拼接

class Solution {

    // 分治 9:30 10:34

    List list = new ArrayList();
    Map> map = new HashMap();

    List diff(int start, int end) {
        int key = start * 100 + end;
        if (map.containsKey(key)) {
            // System.out.println("en");
            return map.get(key);
        }
        if (start == end) {
            String str = list.get(start);
            List ret = new ArrayList();
            ret.add(Integer.parseInt(str));
            map.put(key, ret);
            return ret;
        } 
        List ans = new ArrayList();
        for (var i = start; i <= end; i++) {
            String str = list.get(i);
            if (str.equals("+") || str.equals("-") || str.equals("*")) {
                List left = diff(start, i - 1);
                List right = diff(i + 1, end);
                for (var x: left) {
                    for (var y: right) {
                        if (str.equals("+")) {
                            ans.add(x + y);
                        } else if(str.equals("-")) {
                            ans.add(x - y);
                        } else if (str.equals("*")) {
                            ans.add(x * y);
                        }
                    }
                }
            }
        }
        map.put(key, ans);
        return ans;
    }

    public List diffWaysToCompute(String expression) {
        StringBuilder sb = new StringBuilder();
        for (var i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if (c >= '0' && c <= '9') {
                sb.append(c);
            } else {
                list.add(sb.toString());
                sb = new StringBuilder();
                list.add(String.valueOf(c));
            }
        }
        list.add(sb.toString());
        return diff(0, list.size() - 1);
    }
}

 

 

 

你可能感兴趣的:(LeetCode,leetcode,分治)