LeetCode282. Expression Add Operators

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", "1*2*3"]

Example 2:

Input: num = "232", target = 8
Output: ["2*3+2", "2+3*2"]

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: []

分析

解法是利用递归回溯来遍历所有的可能,但是要注意一些边界情形。

public class Solution {
    public List<String> addOperators(String num, int target) {
        List<String> rst = new ArrayList<String>();
        if(num == null || num.length() == 0) return rst;
        helper(rst, "", num, target, 0, 0, 0);
        return rst;
    }  // eval记录当前计算结果,multed计算上次计算变化的部分,在选择乘法时会用到这个
    public void helper(List<String> rst, String path, String num, int target, int pos, long eval, long multed){
        if(pos == num.length()){
            if(target == eval)
                rst.add(path);
            return;
        }
        for(int i = pos; i < num.length(); i++){
            if(i != pos && num.charAt(pos) == ‘0‘) break;  // 抛弃以0开始的数字
            long cur = Long.parseLong(num.substring(pos, i + 1));
            if(pos == 0){
                helper(rst, path + cur, num, target, i + 1, cur, cur);  // 起始数字特殊处理
            }
            else{
                helper(rst, path + "+" + cur, num, target, i + 1, eval + cur , cur);  // 对当前数字cur选择加上之前的部分
                helper(rst, path + "-" + cur, num, target, i + 1, eval -cur, -cur);  // 选择减        // 选择乘法要特殊处理,减去上次变化的部分,将这个变化的部分乘以当前的数字再加上去
                helper(rst, path + "*" + cur, num, target, i + 1, eval - multed + multed * cur, multed * cur );
            }
        }
    }
}

原文地址:https://www.cnblogs.com/f91og/p/9716441.html

时间: 2024-08-27 16:05:54

LeetCode282. Expression Add Operators的相关文章

leetcode282 - 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 = 6Output: [&quo

[LeetCode][JavaScript]Expression Add Operators

Expression Add Operators 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. Examples: "123", 6 -> [

[LeetCode] Expression Add Operators 表达式增加操作符

Given a string that contains only digits 0-9 and a target value, return all possibilities to add operators +, -, or * between the digits so they evaluate to the target value. Examples: "123", 6 -> ["1+2+3", "1*2*3"] "

【LeetCode】282. Expression Add Operators

题目: 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. Examples: "123", 6 -> ["1+2+3", &q

[string]Expression Add Operators

Total Accepted: 5561 Total Submissions: 26048 Difficulty: 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

Expression Add Operators

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. Examples: "123", 6 -> ["1+2+3", "1

[LeetCode] Expression Add Operators

This post shares a very nice solution, which is rewritten below. 1 class Solution { 2 public: 3 vector<string> addOperators(string num, int target) { 4 int n = num.length(); 5 if (!n) return {}; 6 vector<string> ans; 7 for (int i = 1; i <=

282. Expression Add Operators

解题思路:深搜 遍历所有可能的情况,终止条件为表达式计算的结果等于target.注意,这里要使用long 类型保存整数,否则会溢出.我在vs2013,win32模式下使用long同样不能通过 "3456237490", 9191 -> [] 这个案例,改用long long 才可以. 具体解析可以参考文末的这篇文章,讲的很好,主要要注意乘法情况下如何计算表达式的值. class Solution { public: void compute(vector<string>

282 Expression Add Operators 给表达式添加运算符

给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+2+3", "1*2*3"] "232", 8 -> ["2*3+2", "2+3*2"]"105", 5 -> ["1*0+5","10-5"]&