[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.
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 表达式添加运算符的更多相关文章
- [LeetCode] 282. Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- LeetCode 282. Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- 282 Expression Add Operators 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...
- [LeetCode] Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...
- 【LeetCode】282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- Java实现 LeetCode 282 给表达式添加运算符
282. 给表达式添加运算符 给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+.- 或 * ,返回所有能够得到目标值的表达式. 示例 1: 输入: num = ...
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- leetcode 282. 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况. 例如: "123", 6 -> [" ...
随机推荐
- MySQL通过分组计算百分比
公司在做柯米克的分析报告,需要我这边把汽车之家柯米克论坛的评论数据和评论用户所在地的数据获取,通过爬虫的方式很快的解决了数据的问题,但是需要我提取下各省评论人数的比例,所以在数据库里面直接计算了相关的 ...
- Eclipse配置Python的IDE
我第一个用来实际应用的编程语言是Java,于是对Eclipse情有独钟.但是自从上手了Notepad++后,使用Eclipse的机会越来越少. 最近开始学习Python,因为对Python不太熟悉,有 ...
- 基于MATLAB System Generator 搭建Display Enhancement模型
基于MATLAB System Generator 搭建Display Enhancement模型
- signapk
signapk工具可以实现对安卓ROM和安卓应用进行签名.在安卓DIY与安卓ROM制作中作用是非常大的.可以使用其对经过自己DIY修改美化后的应用进行签名或对制作好的安卓ROM卡刷包进行签名.让我们做 ...
- oracle user_tables没有新创建的表的问题
oracle 新创建表后,在user_tables没有,在user_tab_columns也没有,暂时未找到办法
- nginx支持ipv6
今天碰到的问题是nginx对于ipv6的请求没有日志,顺便查了一下,nginx对ipv6的支持. nginx -v查看nginx是否支持ipv6,出现--with-ipv6,则是支持nginx的,否则 ...
- 涂抹mysql笔记-mysql字符集
字符集:查看mysql数据库当前都支持哪些字符集:system@(none)>show character set;+----------+--------------------------- ...
- 用GDB调试程序(七)
改变程序的执行——————— 一旦使用GDB挂上被调试程序,当程序运行起来后,你可以根据自己的调试思路来动态地在GDB中更改当前被调试程序的运行线路或是其变量的值,这个强大的功能能够让你更好的调试你的 ...
- python基础之数字、字符串、列表、元组、字典
Python基础二: 1.运算符: 判断某个东西是否在某个东西里面包含: in 为真 not in 为假 (1).算术运算符: 运算符 描述 实例 + 加 表示两个对象相加 a + b输出结果3 ...
- Python与设计模式之创建型模式及实战
用Python学习一下设计模式,如果很枯燥的话,就强行能使用的就用一下.设计模式参考Python与设计模式-途索 1. 单例模式 保证一个类仅有一个实例,并提供一个访问它的全局访问点. import ...