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 );
}
}
}
}

LeetCode282. Expression Add Operators的更多相关文章

  1. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  2. Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  3. LeetCode Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

  4. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  5. [leetcode]282. Expression Add Operators 表达式添加运算符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  6. 【LeetCode】282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

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

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  8. LeetCode 282. Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

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

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...

随机推荐

  1. RabbitMQ 中 Connection 和 Channel 详解

    我们知道无论是生产者还是消费者,都需要和 RabbitMQ Broker 建立连接,这个连接就是一条 TCP 连接,也就是 Connection. 一旦 TCP 连接建立起来,客户端紧接着可以创建一个 ...

  2. centos systemctl daemon-reload 提示 no such file or directory 的一个原因

    service 的文件名写错了 比如 mongodb.service 写成了 mongodb.srvice 真的是坑,居然没有提示具体的路径,只是提示一个 no such file or direct ...

  3. Kubernetes 1.5 配置dns

    在kubernetes1.2的时候,采用了skydns + kube2dns +etcd的方式来部署dns.而从1.3开始,则部署方式有了一点儿变化,将skydns和kube2dns封装到了一个容器镜 ...

  4. 又一家药企IPO被拒,原因竟然是……

    版权所有,未经授权不得转载,QQ:231469242 导读 近日,中国证监会官网发布公告,河南润弘制药首发未IPO能通过,成为今年第4家IPO被否的制药企业.中国证监会拒绝润弘制药的原因是润弘制药产品 ...

  5. group by实现原理及其作用

    mysql中group by实现方式有三种,松散索引,紧凑索引,临时文件(文件排序). 在网上看了相关的介绍,大部分介绍都比较晦涩难懂,这里说下我的理解. 在学习SQL优化时,我们都知道可以对grou ...

  6. windows下静态编译pthread

    1. Building the library as a statically linkable library-------------------------------------------- ...

  7. SVN不能提交代码

    Error: Some resources were not reverted. Attempted to lock an already-locked dir svn: Working copy ' ...

  8. WebStorm 使用webpack打包(build) Vue 静态资源无法访问(路径不对)问题

    在WebStorm中使用webpack打包 (命令npm run build) 后生成在项目的dist目录下,在浏览器打开,静态资源js.css等无法加载.因为打包时,资源使用了绝对路径. 解决: 打 ...

  9. CSS3实战之box-shadow篇

    box-shadow属性包含6个参数值:阴影类型.X轴位移.Y轴位移.阴影大小.阴影扩展和阴影颜色.这6个参数值可以有选择地省略. 现在我们用一个img元素来举栗子 我们先来写最简单的box-shad ...

  10. Getting Real 摘记

    第二章 起始点 一个很好的做软件的方式就是一开始用它来解决你自己的问题.由于你自己变成了软件的目标受众因此你会知道什么是重要的什么不是.这样做下去将会是推出一个突破性产品的伟大起始点. 手头有多少钱就 ...