LeetCode 282. Expression Add Operators(dfs)

282. Expression Add Operators

Hard

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

Example 1:

Input: num = “123”, target = 6
Output: [“1+2+3”, “123”]
Example 2:

Input: num = “232”, target = 8
Output: [“23+2", "2+32”]
Example 3:

Input: num = “105”, target = 5
Output: [“1*0+5”,“10-5”]
Example 4:

Input: num = “00”, target = 0
Output: [“0+0”, “0-0”, “0*0”]
Example 5:

Input: num = “3456237490”, target = 9191
Output: []

题意

在一个由数字组成的字符串中插入+/-/*,使得算式的计算结果等于整数target,返回所有可行的算式列表

思路

dfs,搜索遍历所有的算式,分别求结果与target比较

代码

class Solution {
    private List<String> ans = new ArrayList<String>();
    
    /**
    * @param num
    * @param target
    * @param pos Add op after num[pos]
    */
    private void dfs(String num, int target, int pos, String expr) {
        int n = num.length();
        if (pos == n) {
            if (eval(expr, target)) {
                ans.add(expr);
            }
            return;
        }
        expr += num.substring(pos, pos+1);
        if (pos < n-1) {
            dfs(num, target, pos+1, expr + '+');
            dfs(num, target, pos+1, expr + '-');
            dfs(num, target, pos+1, expr + '*');
        }
        dfs(num, target, pos+1, expr);
    }
    
    /**
    * @param s Expression containing +, -, * for evaluation
    * @param target evaluation target
    * @return If the value of {@code s} equals {@code target}
    */
    private boolean eval(String s, int target) {
        long num = 0, num1 = -1, num2 = -1;
        char op1 = 0, op2 = 0, pre = 0;
        boolean sign = true;
        for (char ch: s.toCharArray()) {
            if (ch == 0) {
                continue;
            } else if (ch >= '0' && ch <= '9') {
                if (pre == '0' && num == 0) {
                    return false;
                }
                num = num * 10 + ch - '0';
            } else if (ch == '+' || ch == '-' || ch == '*') {
                if (op2 == '*') {
                    num2 *= num;
                    op2 = ch;
                } else if (op2 == 0 && op1 == '*') {
                    num1 *= num;
                    op1 = ch;
                } else if (op2 == '+' && (ch == '+' || ch == '-')) {
                    num2 += num;
                    op2 = ch;
                } else if (op2 == '-' && (ch == '+' || ch == '-')) {
                    num2 -= num;
                    op2 = ch;
                } else if (op2 == 0 && op1 == '+' && (ch == '+' || ch == '-')) {
                    num1 += num;
                    op1 = ch;
                } else if (op2 == 0 && op1 == '-' && (ch == '+' || ch == '-')) {
                    num1 -= num;
                    op1 = ch;
                } else if (op1 == 0) {
                    num1 = num;
                    op1 = ch;
                } else if (op2 == 0) {
                    num2 = num;
                    op2 = ch;
                } 
                if (op2 == '+' || op2 == '-') {
                    switch (op1) {
                        case '+':
                            num1 += num2;
                            break;
                        case '-':
                            num1 -= num2;
                            break;
                        case '*':
                            num1 *= num2;
                            break;
                    }
                    op1 = op2;
                    op2 = 0;
                }
                num = 0;
            }
            pre = ch;
        }
        if (op1 == 0) {
            return num == (long)target;
        } else if (op2 == 0) {
            switch (op1) {
                case '+':
                    return num1 + num == (long)target;
                case '-':
                    return num1 - num == (long)target;
                case '*':
                    return num1 * num == (long)target;
            }
        } else {
            switch (op2) {
                case '*':
                    num2 *= num;
                    break;
            }
            switch (op1) {
                case '+':
                    return num1 + num2 == (long)target;
                case '-':
                    return num1 - num2 == (long)target;
            }
        }
        return false;
    }
    
    public List<String> addOperators(String num, int target) {
        dfs(num, target, 0, "");
        return ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值