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

题目

给定一个数字串S和一个值target,允许你在S中间添加加减乘符号,使得表达式结果为target,求所有添法。

思路

dfs + pruning(适当剪枝)

代码

   class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<>();
dfs(num, 0, 0, 0, "", res, target);
return res;
} private void dfs(String num, int index, long sum, long last, String s, List<String> res, int target) { if(index == num.length()) {
if(sum == target) {
res.add(s);
}
} for(int i = index + 1; i <= num.length(); i++) {
String temp = num.substring(index, i);
if(temp.length() > 1 && temp.charAt(0) == '0') {
continue;
} long n = Long.valueOf(temp); if(index == 0) {
dfs(num, i, sum + n, n, s + n, res, target);
continue;
}
dfs(num, i, sum + n, n, s + "+" + n, res, target);
dfs(num, i, sum - n, -n, s + "-" + n, res, target);
dfs(num, i, (sum-last) + last * n, last * n, s + "*" + n, res, target);
}
}
}

[leetcode]282. Expression Add Operators 表达式添加运算符的更多相关文章

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

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

  2. LeetCode 282. Expression Add Operators

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

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

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

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

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

  5. 【LeetCode】282. Expression Add Operators

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

  6. 282. Expression Add Operators

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

  7. Java实现 LeetCode 282 给表达式添加运算符

    282. 给表达式添加运算符 给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+.- 或 * ,返回所有能够得到目标值的表达式. 示例 1: 输入: num = ...

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

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

  9. leetcode 282. 给表达式添加运算符

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

随机推荐

  1. lvm快照

    磁盘快照的使用(快照好像只可以使用一次,用过后自动删除) 首先在 /bplvm 下随便创建一个文件,如a.txt 然后执行命令 lvcreate -L 120M -s -n SNAP /dev/sto ...

  2. 记一次php脚本memory exhausted

    表象报错如下: Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16651985 bytes) 出 ...

  3. MySQL 列,可选择的数据类型(通过sql命令查看:`help create table;`)

    MySQL 列,可选择的数据类型(通过sql命令查看:help create table;) BIT[(length)] | TINYINT[(length)] [UNSIGNED] [ZEROFIL ...

  4. DDR3初识

    DDR3初识 选择2:1 ratio 意味用户总线宽度为DDR物理数据接口宽度的4倍.

  5. Linux查看端口占用情况并释放端口占用

    1.netstat -tunlp:查看所有tcp/udp端口占用及进程相关信息 2.netstat -tln | grep 端口号:查看特定端口占用情况 3.kill -9 进程ID(PID):释放指 ...

  6. 三元运算&匿名函数lambda

    lambda # 语法: # 参数 : 返回值 # 1.不带参数的lambda表达式 def func(): return '开挂的人生不需要解释' func = lambda : '开挂的人上不需要 ...

  7. docker上搭建consul集群全流程

    consul简介: consul是提供服务发现.简单配置管理.分区部署的服务注册发现解决方案.主要特性:服务发现\健康检查\基于Key-Value的配置\支持TLS安全通讯\支持多数据中心部署 con ...

  8. [持续交付实践] pipeline使用:快速入门

    什么是pipeline 先介绍下什么是Jenkins 2.0,Jenkins 2.0的精髓是Pipeline as Code,是帮助Jenkins实现CI到CD转变的重要角色.什么是Pipeline, ...

  9. PyQt--QTreeWidget

    转载:loster_Li QTreeWidget的继承关系如下图: 因为继承关系是 QAbstractItemView->QTreeView->QTreeWidget  ,所以和QTabl ...

  10. RF:操作笔记

    1.变量运算