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. UltraISO 9.7.1.3519注册码

    王涛 7C81-1689-4046-626F redcaps 82C6-3DEF-AB07-0EC0

  2. MVC 模式

    1.MVC 模式简介 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发.Model(模型):模型代表一个存取数据的对象或 JAV ...

  3. ubuntu18.04安装openresty

    ubuntu18.04使用openresty官方APT源安装openresty 添加openresty的 APT 仓库,这样就可以便于未来安装或更新软件包(通过 apt-get update 命令). ...

  4. python实现Content-Type类型为application/x-www-form-urlencoded发送POST请求

    周日晚上接到公司的电话需要通过新榜的接口拿下微博热搜数据,拿到接口文档看了下很简单的一个post请求,主要根据时间段来获取热搜数据. 在实际编码的过程中经常遇到header的Content-Type的 ...

  5. Linux之find

    命令功能: find命令是用来在给定的目录下查找符合给定条件的文件.它需要从磁盘中查找,效率低,whereis和locate是基于缓存中数据库查找,效率很高,但是一些新建的文件可能未加入到数据库中,使 ...

  6. 记一次nginx403错误

    同事开发微信小程序,小程序通过API接口调用我们的人脸比对API,但是一直是提示403,通过查看查看nginx日志,发现请求并没有转发出去,转发出去的请求,应该是301,重定向, 然后就开始在ngin ...

  7. 关于CoreData的用法

    有些同事觉得CoreData是一个看不懂,理解不清的神秘东东,其实ios的本地数据储存是一个sqlite数据库,一个简易的数据库,而这个CoreData是否支持所有储存的数据呢,显然不是的,站在我的角 ...

  8. 论文 | YOLO(You Only Look Once)目标检测

    论文:You Only Look Once: Unified, Real-Time Object Detection 原文链接:https://arxiv.org/abs/1506.02640 背景介 ...

  9. python-web微信实现

    1.url from django.conf.urls import url from django.contrib import admin from web import views urlpat ...

  10. centos7 设置系统默认启动的界面

    系统默认 以某种方式启动 使用systemd创建符号链接指向默认运行级别. 修改方法为:在root下 1.首先删除已经存在的符号链接rm /etc/systemd/system/default.tar ...