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. django基础 -- 10.form , ModelForm ,modelformset

    一.生成页面可用的 HTML标签 1.form 所有内置字段 Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label ...

  2. oracle删除当前用户以及当前用户所有表、索引等操作

    ORACLE删除当前用户下所有的表的方法 如果有删除用户的权限,则可以: drop user user_name cascade; 加了cascade就可以把用户连带的数据全部删掉.删除后再创建该用户 ...

  3. 第2章 Java基本语法(下): 流程控制--项目(记账本)

    2-5 程序流程控制 2-5-1 顺序结构 2-5-2 分支语句1:if-else结构 案例 class IfTest1{ public static void main(String[] args) ...

  4. nginx里proxy_pass有无/的区别

    nginx在反向代理的时候,proxy_pass需要指定路径,有无"/"的区别,如下:   location /lile { 配置一: proxy_pass http://192. ...

  5. 彻底放弃没落的MFC,对新人的忠告!--吃瓜群众围观撕逼

    http://bbs.csdn.net/topics/391817496 完全没想到10多年后还有人纠结要不要学MFC,我花点时间给新人们一个总结. 第1种观点 学习完MFC,你会更理解编程的思想,再 ...

  6. 基础 - 字符读取函数scanf、getchar、gets、cin(清空缓存区解决单字符回车问题)

    0x01 scanf.getchar.cin读取单字符: 如下: //scanf读取字符 回车问题 void Sub_1_1() { char v1,v2; scanf("%c", ...

  7. ORACLE procedure 一个参数分解成多个字符一点建议

    测试时给什么变量就会生成什么变量, 但是在PROCEDURE时,你给的变量就会变成去掉包含字符q'/ /' 使用procedure splice添加字符串结果,是不包含q'/.删除时用的riqi赋值语 ...

  8. Quartz的API简介及Jobs和Trigger介绍

    Quartz的API: 主要api: The key interfaces of the Quartz API are: Scheduler - the main API for interactin ...

  9. (转)SQLServer查询数据库各种历史记录

    原文地址https://www.cnblogs.com/seusoftware/p/4826958.html 在SQL Server数据库中,从登陆开始,然后做了什么操作,以及数据库里发生了什么,大多 ...

  10. centos7 安装gitlab任意版本

    主要还是根据官网:https://www.gitlab.cc/installation/#centos-7 1.安装依赖: sudo yum install curl policycoreutils ...