题目:

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.

Examples:

"123", 6 -> ["1+2+3", "1*2*3"]
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []

链接: http://leetcode.com/problems/expression-add-operators/

题解:

给定字符串和target num,求添加不同的运算符,使得字符串经过计算的值等于target num,要求所有的结果。 这道题好像以前小时候玩的数字游戏啊, 现在能用程序解决,真的很高兴。 代码主要参考了discuss里czonzhu的。要处理好几种边界条件,并且这里是用String进行cache,换成StringBuilder的话可能要多写不少行。需要注意的地方是,当i != position,并且position这个char的值为0时,我们不考虑接下来的运算,直接break,这个处理了case: "2" + "05"之类的。还有当position == "0"时,我们不加任何运算符号。因为有乘法运算,所以我们要cache之前的数字, 在backtracking的时候可以方便计算。  这样算下来,是否用doubly linked list来回溯可以处理更多的case,二刷时要尝试。

Time Complexity - (4n), Space Complexity - (4n)

public class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<>();
if(num == null || num.length() == 0) {
return res;
}
addOperators(res, "", num, target, 0, 0, 0);
return res;
} private void addOperators(List<String> res, String path, String numString, int target, int position, long curNum, long lastVal) {
if(position > numString.length()) {
return;
}
if(position == numString.length() && curNum == target) {
res.add(path);
return;
} for(int i = position; i < numString.length(); i++) {
if(i != position && numString.charAt(position) == '0') { // pruning edge case: "2" + "05", wrong cut
break;
}
long curVal = Long.parseLong(numString.substring(position, i + 1));
if(position == 0) {
addOperators(res, path + curVal, numString, target, i + 1, curNum + curVal, curVal);
} else {
addOperators(res, path + "+" + curVal, numString, target, i + 1, curNum + curVal, curVal);
addOperators(res, path + "-" + curVal, numString, target, i + 1, curNum - curVal, -curVal);
addOperators(res, path + "*" + curVal, numString, target, i + 1, curNum - lastVal + lastVal * curVal, lastVal * curVal);
}
}
}
}

Reference:

https://leetcode.com/discuss/58614/java-standard-backtrace-ac-solutoin-short-and-clear

https://leetcode.com/discuss/58535/17-lines-solution-dfs-c

https://leetcode.com/discuss/58547/accepted-c-solution

https://leetcode.com/discuss/58876/ac-solution-c-short

https://leetcode.com/discuss/61975/elegant-java-solution

https://leetcode.com/discuss/70597/clean-python-dfs-with-comments

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

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

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

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

  4. LeetCode 282. Expression Add Operators

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

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

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

  6. Expression Add Operators

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

  7. LeetCode Expression Add Operators

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

  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. LeetCode282. Expression Add Operators

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

随机推荐

  1. xml基础学习笔记02

    接着上次的xml笔记01 ,记录一下如何DOM创建xml 一.由于没有特意来写博客,都是在程序中用注释的方法记录笔记,整理过来难免格式错误,排版什么的,大家好事看不懂就仔细看给出的代码加上注释,一定会 ...

  2. PVPGN1.8.2 + D2GS1.11(38)搭建暗黑破坏神1.11b战网(配置指南)

    首先介绍一下PVPGN和D2GS,PVPGN是一个多人网络游戏平台,全称是(Player vs Player Gaming Network),它的前身是Bnetd.Bnetd由于吃到暴雪的官司败诉,最 ...

  3. Oracle VM VirtualBox 5.0 CentOS 6.4 共享文件夹

    首先在主机(win7)的硬盘建立需要共享文件夹 例如 D:\share_test 然后虚拟机光驱加载Oracle VM VirtualBox安装目录的iso  C:\Program Files\Ora ...

  4. 使用HTML5中postMessage实现Ajax中的POST跨域问题

    HTML5中提供了在网页文档之间相互接收与发送信息的功能.使用这个功能,只要获取到网页所在窗口对象的实例,不仅仅同源(域+端口号)的web网页之间可以互相通信,甚至可以实现跨域通信. 浏览器支持程度: ...

  5. VIM 技巧 (一)全文统一添加

    大家应该有遇到过给整篇内容增加同样的东西的经历.例如给每行结尾增加分号.冒号等内容. 今天和大家分享下 关于此场景如何快速.高效的实现. 例如 Line one Line two Line three ...

  6. Windows7 64位安装配置Apache2.4+PHP5.4+MySQL5.5+Xdebug

    PHP更新已经到了5.4.7了,之前是用PHPstudy安装的PHP5.2.13版本,今天有空,就把之前的集成安装卸载了.换上了新一代PHP,记录一下.. 环境:Windows7 64位(内部版本76 ...

  7. frequentism-and-bayesianism-chs-ii

    frequentism-and-bayesianism-chs-ii 频率主义 vs 贝叶斯主义 II:当结果不同时   这个notebook出自Pythonic Perambulations的博文  ...

  8. css 内联元素inline 行框全解

    首先看一篇文章: CSS框模型:一切皆为框 — 从行框说起 一 行框 看图说话 上图代表了框模型中的行框.line-height 属性设置行间的距离(行高).该属性会影响行框的布局.在应用到一个块级元 ...

  9. Sqli-labs less 34

    Less-34 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理.由上面的例子可以看到我们的方法就是将过滤函数添加的 \ 给吃掉.而get型的方式我们是以url形式提交 ...

  10. ID3决策树---Java

    1)熵与信息增益: 2)以下是实现代码: //import java.awt.color.ICC_ColorSpace; import java.io.*; import java.util.Arra ...