LeetCode282. 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: []
分析
解法是利用递归回溯来遍历所有的可能,但是要注意一些边界情形。
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的更多相关文章
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- LeetCode Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- [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
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- [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 ...
- [LeetCode] Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...
随机推荐
- supervisor 通过图形界面管理
编辑 supervisor 配置文件, [inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ; ip_addr ...
- 修改const保护的值
先看代码: #include <stdio.h> void main() { const int num = 10; int *a = (int *)(void *)# / ...
- python与pycharm
什么叫自动化测试? 通俗来说,自动化测试就是通过写代码来帮我们测试软件.用来做自动化测试的语言很多,python,Java,php,Go,ruby等.而且软件系统开发语言与自动化测试语言可以不一致.例 ...
- 使用tkinter做简单计算器
代码如下: from tkinter import * #导入tkinter库 root =Tk() #给窗体 root.title('calculator') #设置窗体名字 frm=Frame(r ...
- 设置PyCharm中的Python代码模版
再MacOs运行的PyCharm中,执行python文件,如果不指定python文件字符编码会报错: SyntaxError: Non-ASCII character , but no encodin ...
- [Luogu 3275] SCOI2011 糖果
[Luogu 3275] SCOI2011 糖果 第一道差分约束.感谢 AZe. 我的理解是根据一些不等关系建一个图,在图上边跑一个最长路(有时候是最短路). 因为可能存在负环,所以必须用 SPFA! ...
- Django 2.0.1 官方文档翻译: 快速安装向导 (Page5)
快速安装向导 (Page 5) 在你使用 Django 前,你需要先安装它.我们有一个完整的安装向导,它包含所有涉及的内容,这个向导会指导你进行一个简单的.最小化的安装,当你通过浏览介绍内容的时候,这 ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- JavaScript语法对{}的奇葩处理
JavaScript的语法有多坑,算是众人皆知了. 今天看到vczh的这条微博:http://weibo.com/1916825084/B7qUFpOKb , 代码如下: {} + []; [] + ...
- 在windows的IDEA运行Presto
After building Presto for the first time, you can load the project into your IDE and run the server. ...