282. Expression Add Operators (Hard) 考简化版

Given a string that contains only digits0-9and a target value, return all possibilities to add binary operators (not unary)+,-, or*between the digits so they evaluate to the target value.

Examples:

"123", 6 -> ["1+2+3", "1*2*3"] 
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []
Solution 1: DFS O(n * 4^n); O(n)
    public List<String> addOperators(String num, int target) {
        List<String> res = new ArrayList<>();
        if (num == null || num.length() == 0) {
            return res;
        }

        StringBuilder sb = new StringBuilder();
        dfs(res, sb, num.toCharArray(), 0, target, 0, 0);
        return res;
    }

    private void dfs(List<String> res, StringBuilder sb, char[] num, int pos, int target, long prev, long multi) {
        if (pos == num.length) {
            if (prev == target) {
                res.add(sb.toString());
            }
            return;
        }

        long curr = 0;
        for (int i = pos; i < num.length; i++) {
            if (num[pos] == '0' && i != pos) {
                break;
            }

            curr = curr * 10 + num[i] - '0';
            int len = sb.length();
            if (pos == 0) {
                dfs(res, sb.append(curr), num, i + 1, target, curr, curr);
                sb.setLength(len);
            } else {
                dfs(res, sb.append('+').append(curr), num, i + 1, target, prev + curr, curr);
                sb.setLength(len);
                dfs(res, sb.append('-').append(curr), num, i + 1, target, prev - curr, -curr);
                sb.setLength(len);
                dfs(res, sb.append('*').append(curr), num, i + 1, target, prev - multi + multi * curr , multi * curr);
                sb.setLength(len);
            }
        }
    }
Follow Up:

1.简化版1:123456789=100, 在等式左边任意位置加上“-”或者“+”使得等式成立。Print all possible combinations. 如: 123 + 456 + 78 -9 是1种组合, -1 + 2 -3 +4 -5 - 67 + 89 也是1种(只加 + 或 -)

    public void addOperators(String num) {
        if (num == null) {
            System.out.println("null");
        }

        dfs(new StringBuilder(), num, 0, 100, 0);
    }

    private void dfs(StringBuilder sb, String num, int start, int target, long pre) {//target can be omitted
        if (start == num.length()) {
            if (pre == target) { //previous calculated result
                System.out.println(sb.toString());
            }
            return;
        }

        long cur = 0;
        for (int i = start; i < num.length(); i++) {
            //0 sequence: because we can't have numbers with multiple digits started with zero, stop further dfs
            if (num.charAt(start) == '0' && i != start) {
                break;
            }

            int len = sb.length();
            cur = cur * 10 + num.charAt(i) - '0'; //long cur = Long.parseLong(num.substring(pos, i + 1));//bad
            if (start == 0) {
                dfs(sb.append(cur), num, i + 1, target, cur);
                sb.setLength(len);
            } else {
                dfs(sb.append('+').append(cur), num, i + 1, target, pre + cur);
                sb.setLength(len);
                dfs(sb.append('-').append(cur), num, i + 1, target, pre - cur);
                sb.setLength(len);
            }
        }
    }

2.简化版2:求出结果可以是0的公式数量。

用一个全局变量计数即可。

results matching ""

    No results matching ""